相当简单awk
:
awk '(NR%3)' awk.write
对于这个文件:
this line 1 no un1x
this lines 22 0
butbutbut this 33 22 has unix
but not 1
THIS is not
butbutbut ffff
second line
我的输出为:
this line 1 no un1x
this lines 22 0
but not 1
THIS is not
second line
但最后一行是不需要的,因为它不符合连续的定义。
我怎样才能获得每三连续行的前两行?
答案1
您可以使用变量来跟踪上一行是否存在:
$ awk '
FNR % 3 == 1 {f = $0; next} # The first line keep in f, skip to next line
FNR % 3 && f {print f;print} # Previous line present, print it and current line
' <file
this line 1 no un1x
this lines 22 0
but not 1
THIS is not
或者与sed
:
sed -ne 'N;/\n/p;N;d' <file