AWK:是否有一些标志可以忽略评论?

发布于 2024-08-29 23:02:34 字数 812 浏览 3 评论 0原文

评论行数计入 NR。

  1. 是否有一些标志可以忽略评论?
  2. 如何限制 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.

  1. Is there some flag to ignore comments?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黯然#的苍凉 2024-09-05 23:02:34

最好不要碰 NR ,使用不同的变量来计算行数。此版本跳过注释和空行。

$ awk '!/^[ \t]*#/&&NF{sum+=$3;++d}END{ave=sum/d;print ave}' file
0.97404

it is best not to touch NR , use a different variable for counting the rows. This version skips comments as well as blank lines.

$ awk '!/^[ \t]*#/&&NF{sum+=$3;++d}END{ave=sum/d;print ave}' file
0.97404
断舍离 2024-09-05 23:02:34

只需在注释行上减少 NR 即可:

 awk '/^[[:space:]]*#/ { NR-- } {sum+=$3} END { ... }' coriolis_data

好的,这确实回答了您提出的问题,但您真正的意思是:(

 awk '{ if ($0 ~ /^[[:space:]]*#/) {NR--} else {sum+=$3} END { ... }' coriolis_data

如第一个答案中那样在块外使用模式更加笨拙,但要这样做,你必须写两次评论模式。)

编辑:将在评论中建议使用 /.../ {NR--; next} 以避免出现 if-else 块。我的想法是,当您对匹配记录有更复杂的操作时,这看起来更干净,但对于这么简单的事情来说并不重要。拿走你最喜欢的吧!

Just decrement NR yourself on comment lines:

 awk '/^[[:space:]]*#/ { NR-- } {sum+=$3} END { ... }' coriolis_data

Okay, that did answer the question you asked, but the question you really meant:

 awk '{ if ($0 ~ /^[[:space:]]*#/) {NR--} else {sum+=$3} END { ... }' coriolis_data

(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!

几味少女 2024-09-05 23:02:34

另一种方法是使用条件语句...

awk '{ if( $1 != "#" ){ print $0 } }' coriolis_data

它的作用是告诉 awk 跳过第一个条目是 # 的行。当然,这需要注释字符 # 单独出现在注释的开头。

Another approach is to use a conditional statement...

awk '{ if( $1 != "#" ){ print $0 } }' coriolis_data

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.

甜中书 2024-09-05 23:02:34

有一个更简单的方法可以做到这一点!

$ awk '!/#/ {print $0}' coriolis_data
.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

更正:不,不是!

$ awk '!/#/ {sum+=$3}END{ave=sum/NR}END{print ave}' coriolis_data 
0.885491    // WRONG.
$ awk '{if ($0 ~ /^[[:space:]]*#/){NR--}else{sum+=$3}}END{ave=sum/NR}END{print ave}' coriolis_data
0.97404     // RIGHT.

There is a SIMPLER way to do it!

$ awk '!/#/ {print $0}' coriolis_data
.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

Correction: no, it is not!

$ awk '!/#/ {sum+=$3}END{ave=sum/NR}END{print ave}' coriolis_data 
0.885491    // WRONG.
$ awk '{if ($0 ~ /^[[:space:]]*#/){NR--}else{sum+=$3}}END{ave=sum/NR}END{print ave}' coriolis_data
0.97404     // RIGHT.
倒数 2024-09-05 23:02:34

您提供给 AWK 解析的文件不是源文件,而是数据,因此,AWK 对它的配置一无所知。换句话说,对于 AWK 来说,以 # 开头的行没有什么特别的。

也就是说,您当然可以跳过注释,但您必须为此创建一个逻辑:只需告诉 AWK 忽略“#”后面的所有内容并计算行数。

awk 'BEGIN {lines=0} {if(substr($1, 0, 1) != "#") {sum+=$3; lines++} } END {avg=sum/lines} END {print avg}' coriolis_data

当然,您可以缩进它以提高可读性。

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.

awk 'BEGIN {lines=0} {if(substr($1, 0, 1) != "#") {sum+=$3; lines++} } END {avg=sum/lines} END {print avg}' coriolis_data

You can, of course, indent it for better readability.

别想她 2024-09-05 23:02:34

我会先用 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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文