如何用awk打印模式之后连续5行文件 [英] How to print 5 consecutive lines after a pattern in file using awk
本文介绍了如何用awk打印模式之后连续5行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想搜索一个文件,并发现模式后打印5行的模式。
我需要用awk,为了做到这一点。
例如:
文件内容:
。
。
。
。
####模式#######
#1号线
#2号线
#3号线
#线路4
5号线#
。
。
。
我如何通过文件和打印仅上述线路解析?
我是否使用含有模式的行NR并保持递增高达5打印过程中的每一行。
请不要让我知道是否有任何其他有效的笏做,在awk中。
解决方案
的awk'
{
如果(系大于0){
打印;
--lines;
}
}/ PATTERN / {
线= 5
}'<输入
这产生:
#1号线
#2号线
#3号线
#线路4
5号线#
I would like to search for a pattern in a file and prints 5 lines after finding that pattern. I need to use awk in order to do this.
Example: File Contents:
.
.
.
.
####PATTERN#######
#Line1
#Line2
#Line3
#Line4
#Line5
.
.
.
How do I parse through a file and print only the above mentioned lines? Do I use the NR of the line which contains "PATTERN" and keep incrementing upto 5 and print each line in the process. Kindly do let me know if there is any other efficient wat to do it in Awk.
解决方案
awk '
{
if (lines > 0) {
print;
--lines;
}
}
/PATTERN/ {
lines = 5
}
' < input
This yields:
#Line1
#Line2
#Line3
#Line4
#Line5
这篇关于如何用awk打印模式之后连续5行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文