AWK:是否有一些标志可以忽略评论?
评论行数计入 NR。
- 是否有一些标志可以忽略评论?
- 如何限制 AWK 中的范围,而不是像管道
| sed -e '1d'
,忽略注释行?
示例
$ awk '{sum+=$3} END {avg=sum/NR} END {print avg}' coriolis_data
0.885491 // WRONG divided by 11, should be by 10
$ cat coriolis_data
#d-err-t-err-d2-err
.105 0.005 0.9766 0.0001 0.595 0.005
.095 0.005 0.9963 0.0001 0.595 0.005
.115 0.005 0.9687 0.0001 0.595 0.005
.105 0.005 0.9693 0.0001 0.595 0.005
.095 0.005 0.9798 0.0001 0.595 0.005
.105 0.005 0.9798 0.0001 0.595 0.005
.095 0.005 0.9711 0.0001 0.595 0.005
.110 0.005 0.9640 0.0001 0.595 0.005
.105 0.005 0.9704 0.0001 0.595 0.005
.090 0.005 0.9644 0.0001 0.595 0.005
Comment rows are counted in the NR.
- Is there some flag to ignore comments?
- How can you limit the range in AWK, not like piping
| sed -e '1d'
, to ignore comment rows?
Example
$ awk '{sum+=$3} END {avg=sum/NR} END {print avg}' coriolis_data
0.885491 // WRONG divided by 11, should be by 10
$ cat coriolis_data
#d-err-t-err-d2-err
.105 0.005 0.9766 0.0001 0.595 0.005
.095 0.005 0.9963 0.0001 0.595 0.005
.115 0.005 0.9687 0.0001 0.595 0.005
.105 0.005 0.9693 0.0001 0.595 0.005
.095 0.005 0.9798 0.0001 0.595 0.005
.105 0.005 0.9798 0.0001 0.595 0.005
.095 0.005 0.9711 0.0001 0.595 0.005
.110 0.005 0.9640 0.0001 0.595 0.005
.105 0.005 0.9704 0.0001 0.595 0.005
.090 0.005 0.9644 0.0001 0.595 0.005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最好不要碰
NR
,使用不同的变量来计算行数。此版本跳过注释和空行。it is best not to touch
NR
, use a different variable for counting the rows. This version skips comments as well as blank lines.只需在注释行上减少 NR 即可:
好的,这确实回答了您提出的问题,但您真正的意思是:(
如第一个答案中那样在块外使用模式更加笨拙,但要这样做,你必须写两次评论模式。)
编辑:将在评论中建议使用
/.../ {NR--; next}
以避免出现 if-else 块。我的想法是,当您对匹配记录有更复杂的操作时,这看起来更干净,但对于这么简单的事情来说并不重要。拿走你最喜欢的吧!Just decrement NR yourself on comment lines:
Okay, that did answer the question you asked, but the question you really meant:
(It's more awk-ish to use patterns outside the blocks as in the first answer, but to do it that way, you'd have to write your comment pattern twice.)
Edit: Will suggests in the comments using
/.../ {NR--; next}
to avoid having the if-else block. My thought is that this looks cleaner when you have more complex actions for the matching records, but doesn't matter too much for something this simple. Take your favorite!另一种方法是使用条件语句...
它的作用是告诉
awk
跳过第一个条目是#
的行。当然,这需要注释字符#
单独出现在注释的开头。Another approach is to use a conditional statement...
What this does is tell
awk
to skip lines whose first entry is#
. Of course this requires the comment charactter#
to stand alone at the beginning of a comment.有一个更简单的方法可以做到这一点!
更正:不,不是!
There is a SIMPLER way to do it!
Correction: no, it is not!
您提供给 AWK 解析的文件不是源文件,而是数据,因此,AWK 对它的配置一无所知。换句话说,对于 AWK 来说,以 # 开头的行没有什么特别的。
也就是说,您当然可以跳过注释,但您必须为此创建一个逻辑:只需告诉 AWK 忽略“#”后面的所有内容并计算行数。
当然,您可以缩进它以提高可读性。
The file that you provide for AWK to parse is not a source file, it's data, therefore, AWK knows nothing about its configuration. In other words, for AWK, lines beginning with # are nothing special.
That said, of course you can skip comments, but you will have to create a logic for that: Just tell AWK to ignore everything that comes after a "#" and count yourself the number of lines.
You can, of course, indent it for better readability.
我会先用 sed 删除它们,然后用 grep 删除空行。
sed 's/#.*//' <科里奥利数据 | egrep -v '^$'| awk...
I would remove them with sed first, then remove blank lines with grep.
sed 's/#.*//' < coriolis_data | egrep -v '^$' | awk ...