[英]How to print 5 consecutive lines after a pattern in file using awk
I would like to search for a pattern in a file and prints 5 lines after finding that pattern. 我想在文件中搜索一个模式,并在找到该模式后打印5行。
I need to use awk
in order to do this. 我需要使用awk
才能做到这一点。
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. 我是否使用包含“PATTERN”的行的NR并继续增加到5并在此过程中打印每一行。 Kindly do let me know if there is any other efficient wat to do it in Awk. 如果在Awk中有任何其他有效的扫描仪,请告诉我。
Another way to do it in AWK: 在AWK中执行此操作的另一种方法:
awk '/PATTERN/ {for(i=1; i<=5; i++) {getline; print}}' inputfile
in sed
: 在sed
:
sed -n '/PATTERN/{n;p;n;p;n;p;n;p;n;p}' inputfile
in GNU sed
: 在GNU sed
:
sed -n '/PATTERN/,+7p' inputfile
or 要么
sed -n '1{x;s/.*/####/;x};/PATTERN/{:a;n;p;x;s/.//;ta;q}' inputfile
The #
characters represent a counter. #
字符代表一个计数器。 Use one fewer than the number of lines you want to output. 使用少于您想要输出的行数。
awk '
{
if (lines > 0) {
print;
--lines;
}
}
/PATTERN/ {
lines = 5
}
' < input
This yields: 这会产生:
#Line1
#Line2
#Line3
#Line4
#Line5
grep "PATTERN" search-file -A 5
will do the job for you if decide to give grep a chance. 如果决定给grep一个机会, grep "PATTERN" search-file -A 5
将为你完成这项工作。
Edit: You can call grep using system()
function from inside your awk
script as well. 编辑:您也可以使用awk
脚本中的system()
函数调用grep。
awk
to the rescue! awk
来救援!
to print with the pattern line (total 6 lines) 用图案线打印(共6行)
$ awk '/^####PATTERN/{c=6} c&&c--' file
####PATTERN#######
#Line1
#Line2
#Line3
#Line4
#Line5
to skip pattern and print the next five lines 跳过模式并打印下五行
$ awk 'c&&c--; /^####PATTERN/{c=5}' file
#Line1
#Line2
#Line3
#Line4
#Line5
Edit: didn't notice PATTERN
shouldn't be part of the output. 编辑:没有注意到PATTERN
不应该是输出的一部分。
cat /etc/passwd | awk '{if(a-->0){print;next}} /qmaild/{a=5}'
or 要么
cat /etc/passwd | awk ' found && NR-6 < a{print} /qmaild/{a=NR;found=1}'
The shortest I can come up with is: 我能想到的最短的是:
cat /etc/passwd | awk 'a-->0;/qmaild/{a=5}'
Read as a tends to 0. /qmaild/ sets a to 5 :-) 读取为趋于0. / qmaild /设置为5 :-)
As limited as Johnsyweb's solution but using a different approach and GNU awk
's advanced feature allowing full RegEx record separators resulting in a piece of code that, IMHO, is very awk
-ish: 受限于Johnsyweb的解决方案,但使用不同的方法和GNU awk
的高级功能允许完整的RegEx记录分隔符,导致一段代码,恕我直言,非常awk
-ish:
awk -F\\\n '{NF=5}NR-1' RS="PATTERN[^\n]*." OFS=\\\n
So if you want a stable solution that is also readable, go along with Dennis Williamson's answer (+1 for that one) but maybe you too simply enjoy the beauty of awk
when looking at lines like this. 因此,如果你想要一个同样可读的稳定解决方案,那么请继续使用Dennis Williamson的答案(+1那个),但也许你只是在看这样的线条时享受awk
之美。
Quick&dirty solution: awk '/PATTERN/ {i=0} { if (i<=5) {print $0}; i+=1} BEGIN {i=6}'
快速和肮脏的解决方案: awk '/PATTERN/ {i=0} { if (i<=5) {print $0}; i+=1} BEGIN {i=6}'
awk '/PATTERN/ {i=0} { if (i<=5) {print $0}; i+=1} BEGIN {i=6}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.