假设我有一个shell脚本foo.sh
,它会生成重复的数据集,如下所示:
<name 1>
<address 1>
<mother's name 1>
<pet's name 1>
<comment 1>
<name 2>
<address 2>
<mother's name 2>
<pet's name 2>
<comment 2>
...
我想从每个5行块中提取第1行和第3行,因此修改后的输出如下所示:
<name 1>
<mother's name 1>
<name 2>
<mother's name 2>
...
这些线路没有独特的模式,所以我需要专门按行号过滤。怎么办呢?
答案 0 :(得分:4)
使用GNU sed:
sed -n '1~5p;3~5p' file.txt
来自GNU sed手册:
first~step
Match every step'th line starting with line first. For example, ``sed -n 1~2p''
will print all the odd-numbered lines in the input stream, and the address 2~5
will match every fifth line, starting with the second. first can be zero; in
this case, sed operates as if it were equal to step. (This is an extension.)
答案 1 :(得分:3)
我不是sed
专家,但您可以在Awk中执行此操作:
$ awk '(i==0 || i==2) { print }
{ i = (i+1) % 5 }
' < filename.txt
或者,在一行
$ awk '(NR%5==1 || NR%5==3)' < filename.txt
答案 2 :(得分:1)
您可以使用awk
吗?
awk 'NR % 5 == 1 || NR % 5 == 3' foofile
所以基本上发生的事情是
答案 3 :(得分:0)
这可能对您有用:
sed -n 'h;n;n;H;n;n;g;p' file