Count no of records if no. of fields are not equal to a certain variable Count no of records if no. of fields are not equal to a certain variable unix unix

Count no of records if no. of fields are not equal to a certain variable


Increment the count when NF is not equal to n and print the count at the end:

awk -F"|" -v n=5 'NF!=n{COUNT++} END{print COUNT}'   Input_file


Specific to your file

Assuming

  • VarNumber is a valid integer (not part of the request to secure this)
  • File(s) exists and are readeable

The commented code

awk -F '[|]' -v NotEqualTo="${VarNumber}" '   # if record have number of field NOT equal to given number   NF != NotEqualTo {      # Count the line      c++      }   # at end of file   END {     # print the count (only)     print c     }   ' YourFile

Note:

  • using -F '[|]' to avoid any missunderstanding with the RegEx OR depending option/config used
  • VarNumber is the batch variable with your number of field that are excluded

Generic for lot of file

awk -F '[|]' -v NotEqualTo="${VarNumber}" '   # Count if NF is NOT the number given and put info in array   # 1 entry by file name   NF != NotEqualTo {c[FILENAME]++}   # at end of file, print the count (only) for each file (eement of array)   END { for( f in c) print f " : " c[f] }   ' YourFiles*


this will give you the full distribution of number of fields

$ awk -F\| '{c[NF]++} END{for(i in c) print i ":", c[i]}' file | sort

to recover all but equal to 5, pipe to another awk

$ ... |  awk '!/^5:/{sum+=$2} END{print sum}'