1

I have an "lsusb" output as below:

khalemi@hpx:/opt$ lsusb -d 0c2e:0200
Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner

Q) How can I use "sed" to reformat the output ( using delimiter ---) to be like

Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
---
Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner

I will then pass this output to another process like php script, and explode/split it using (---) delimiter into array.

please help.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
MKhalemi
  • 35
  • 6
  • 1
    Why bother with this when you could just split in PHP on `\n` (newline)? – John Zwinck Feb 10 '15 at 04:49
  • i tried that i doesn't work. but later I found out that i had to put "\n" instead of '\n' in explode() for it to work as expected. my bad – MKhalemi Feb 10 '15 at 05:08

3 Answers3

2

Using awk:

lsusb -d 0c2e:0200 | awk 'NR>1{print "---\n" $0}'

Using sed:

lsusb -d 0c2e:0200 | sed '1!s/^/---\n/'

In both cases, the code works by adding ---\n before every line except for the first line.

John1024
  • 109,961
  • 14
  • 137
  • 171
1

In php, you could do like

Through preg_split

$str = <<<EOT
Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
EOT;
$split = preg_split('~\n~', $str);
print_r($split);

With explode,

$split = explode("\n", $str);

This splits the input according to the newline character.

Output:

Array
(
    [0] => Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
    [1] => Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

another approach

using sed

lsusb -d 0c2e:0200|sed '$!{s/$/&\n---/g}'

results

Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
---
Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
repzero
  • 8,254
  • 2
  • 18
  • 40