Number of fields returned by awk

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

    If you would like to know the set of all the numbers of fields in a multiline content you can run:

    X | awk '{print NF}' | sort -n | uniq
    

    being X a command that outputs content in the standard output: cat, echo, etc. Example:

    With file.txt:

    a b
    b c
    c d
    e t a
    e u
    

    The command cat file.txt | awk '{print NF}' | sort -n | uniq will print:

    2
    3
    

    And with file2.txt:

    a b
    b c
    c d
    e u
    

    The command cat file2.txt | awk '{print NF}' | sort -n | uniq will print:

    2
    

提交回复
热议问题