awk命令打印最后5行,如果匹配 [英] awk command to print last 5 rows if matched
本文介绍了awk命令打印最后5行,如果匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML文件。我需要匹配的文本之前打印五类线。
有可能是多个匹配。我使用的是只返回一行以下awk命令。
的awk'/< \\ / ABC> /'file.xml
返回
< ABC>有的文本1< / ABC>
< ABC>有的文本2< / ABC>
< ABC>有的文本3< / ABC>
预期:
... previous 4行...
< ABC>有的文本1< / ABC>
... previous 4行...
< ABC>有的文本2< / ABC>
... previous 4行...
< ABC>有的文本3< / ABC>
不能使用的grep -B
as每在Solaris中的:
的grep:非法选项 - B的grep:非法选项 - 2用法:grep的-hblcnsviw特征码文件
块引用>解决方案这件怎么样?
的awk'(NR大于1){{A [NR] = $ 0;如果($ 0〜/< \\ / ABC> /){为(I = NR-4; I&所述; = NR;我++){打印由[i]}}}}input.txt中
I have an xml file. I need to print five lines before the matched text.
There may be multiple matches. I am using following awk command which is returning only one row.
awk '/<\/ABC>/' file.xml
returns
<ABC>Some Text1</ABC> <ABC>Some Text2</ABC> <ABC>Some Text3</ABC>
Expected:
... previous 4 lines ... <ABC>Some Text1</ABC> ... previous 4 lines ... <ABC>Some Text2</ABC> ... previous 4 lines ... <ABC>Some Text3</ABC>
Cannot use
grep -B
as per in Solaris:grep: illegal option -- B grep: illegal option -- 2 Usage: grep -hblcnsviw pattern file
解决方案How about this one?
awk '(NR>1){{a[NR]=$0;if($0~/<\/ABC>/){for(i=NR-4;i<=NR;i++){print a[i]}}}}' input.txt
这篇关于awk命令打印最后5行,如果匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文