awk返回的字段数 [英] Number of fields returned by awk

查看:158
本文介绍了awk返回的字段数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以让awk返回满足字段分隔符条件的字段数?举例来说,我的文件包含

Is there a way to get awk to return the number of fields that met a field-separator criteria? Say, for instance, my file contains

a b c d

因此,awk --field-separator=" " | <something>应该返回4

推荐答案

NF变量设置为输入记录中字段的总数.所以:

The NF variable is set to the total number of fields in the input record. So:

echo "a b c d" | awk --field-separator=" " "{ print NF }"

将显示

4

但是请注意:

echo -e "a b c d\na b" | awk --field-separator=" " "{ print NF }"

将显示:

4
2

希望这会有所帮助,并祝您清醒

Hope this helps, and happy awking

这篇关于awk返回的字段数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆