简体   繁体   中英

hot to put a delimiter into output of “lsusb”

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.

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.

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
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM