如何在某一行之后grep多行

如何在某一行之后grep多行,grep,Grep,我有几个这样的文件: abcd several lines abcd several lines abcd several lines . . . 我想做的(最好使用grep)是在最后一行abcd之后立即获得20行 感谢您的帮助 谢谢您可以这样做: awk '/abcd/ {n=NR} {a[NR]=$0} END {for (i=n;i<=n+20;i++) print a[i]}' file awk'/abcd/{n=NR}{a[NR]=0}END{for(i=n;i使

我有几个这样的文件:

abcd

several lines

abcd

several lines

abcd

several lines

.
.
.
我想做的(最好使用grep)是在最后一行abcd之后立即获得20行

感谢您的帮助

谢谢

您可以这样做:

awk '/abcd/ {n=NR} {a[NR]=$0} END {for (i=n;i<=n+20;i++) print a[i]}' file

awk'/abcd/{n=NR}{a[NR]=0}END{for(i=n;i使用
-a
选项:

-A NUM, --after-context=NUM
      Print NUM lines of trailing context after matching lines.  Places a line
      containing a group separator (--) between contiguous groups of matches.  
      With the -o or --only-matching option, this has no effect and a warning
      is given.
因此:

将为您提供
abcd
行,每行后面加20行。要获得最后21行,请使用
tail

$ grep -A 20 abcd file.txt | tail -21
$ grep -A 20 abcd file.txt | tail -21