Number of fields returned by awk

后端 未结 4 1659
抹茶落季
抹茶落季 2021-02-03 23:50

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

so,

4条回答
  •  攒了一身酷
    2021-02-04 00:34

    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 }"

    will display

    4

    Note, however, that:

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

    will display:

    4
    2

    Hope this helps, and happy awking

提交回复
热议问题