[英]Printing certain number of rows using awk. AND skipping certain rows
I have a simple question, which is almost too simple to find on this forum or on awk learning sites. 我有一个简单的问题,在这个论坛或awk学习网站上几乎找不到。
I have some awk code that matches a line beginning with a number, and prints the 6th column of that line: 我有一些awk代码,该代码与以数字开头的行匹配,并打印该行的第六列:
/^[1-9]/ {
print $6
}
How do I tell it to print only the first 50 rows of the column from the match? 我如何告诉它只打印匹配项中列的前50行?
I tried used my own version of the answers below and I got it to print 50 lines. 我尝试使用以下答案的自己的版本,得出50行。 However, now I am trying to choose which 50 lines I print. 但是,现在我试图选择要打印的50行。 I do this by skipping a line that starts with a number and contains the word 'residue'. 通过跳过以数字开头且包含单词“ residue”的行来完成此操作。 Then I skip 5 lines that start with a number and contain a 'w'. 然后,我跳过了以数字开头并包含“ w”的5行。 This method is working as if I am only skipping the line with residue and prints from the first line starting with a number after that. 这种方法的工作方式就像我只跳过带有残渣的行,并从第一行开始打印,然后从后面的数字开始打印。 Do you know why my 'w's are not being considered. 你知道为什么不考虑我的“ w”吗?
#!/usr/bin/awk -f
BEGIN {
line = 0;
skipW = 0;
}
# Ignore all lines beginning with a number until I find one I'm interested in.
/^[0-9]+ residue/ { next }
# Ignore the first five lines beginning with a number followed by a 'w'.
/^[0-9]+ w/ {
skipW += 1;
if (skipW <= 5) next
}
# For all other lines beginning with a number, perform the following. If we are
# "printing", increment the line count. When we've printed 50 lines turn off
# printing from that point on.
/^[0-9]+/ {
++line
if ((line > 0) && (line <= 50)) print $6
}
Use a match counter as part of your condition: 使用匹配计数器作为条件的一部分:
/^[1-9]/ && matched < 50 {
print $6
matched++
}
You can use a shortcut method also: 您还可以使用快捷方式:
/^[1-9]/ { print $6; matched++ }
matched == 50 { exit }
But this may not always work on a pipline, if the producer command does not handle SIGPIPE
gracefully. 但是,如果生产者命令不能正常处理SIGPIPE
,则这可能并不总是适用于管线。
awk '/^[1-9]/ { if (num_printed++ < 50) print $6 }'
This increments num_printed
each time a match is found and prints out the first 50 such lines, regardless of where the lines are in the files in the input. 每次找到匹配项时,此值将递增num_printed
并打印出前50条此类行,而不管这些行在输入文件中的位置。
This reads through all the input. 这将读取所有输入。 If an early exit is OK, then you can use: 如果可以提前退出,则可以使用:
awk '/^[1-9]/ { print $6; if (++num_printed == 50) exit }'
Note the switch from post-increment to pre-increment. 请注意从后增量到前增量的切换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.