grep拆分行

grep拆分行

你可能从来没听说过grep拆分行,因为通常我们都是使用更方便的awk或稍微麻烦点儿的sed
事实上,grep拆分行可能更简单,如下:
root@bwhhkle:~# cat rows.bak 
1 2 3 4 5 6
root@bwhhkle:~# grep -Po '[^ ]+' rows.bak 
1
2
3
4
5
6
注:'[^ ]+'可以换为'[^\s]+'
原理是grep的-o选项:
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line
每一个被匹配到的内容,都会在单独的输出行上;即每个匹配到的“块”都会单独一行输出显示