Number of fields returned by awk

后端 未结 4 1660
抹茶落季
抹茶落季 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:27

    awk(1) on FreeBSD does not recognize --field-separator. Use -v instead:

    echo "a b c d" | awk -v FS=" " "{ print NF }"
    

    It is a portable, POSIX way to define the field separator.

提交回复
热议问题