[英]hot to put a delimiter into output of “lsusb”
I have an "lsusb" output as below: 我有一个“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) How can I use "sed" to reformat the output ( using delimiter ---) to be like 问)如何使用“sed”重新格式化输出(使用分隔符---)
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. 然后我将这个输出传递给另一个像php脚本的进程,并使用(---)分隔符将其分解为数组。
please help. 请帮忙。
Using awk
: 使用
awk
:
lsusb -d 0c2e:0200 | awk 'NR>1{print "---\n" $0}'
Using sed
: 使用
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. 在这两种情况下,代码的工作原理是在除第一行之外的每一行之前添加
---\\n
。
In php, you could do like 在PHP中,你可以这样做
Through preg_split
通过
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
, 随着
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 使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.