我有一个“lsusb”输出如下:

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)如何使用“sed”将输出(使用分隔符---)重新格式化为
Bus 002 Device 004: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner
---
Bus 002 Device 006: ID 0c2e:0200 Metrologic Instruments Metrologic Scanner

然后,我将这个输出传递给另一个进程,比如php脚本,并使用(--)分隔符将其分解/拆分为数组。
请帮忙。

最佳答案

在php中,您可以
通过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);

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

这将根据换行符拆分输入。
输出:
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
)

08-06 23:38