Count the number of fields per line

From raju


The task here is to count the number of fields in each line of a file. Consider for example

    % cat isp.txt 
    # Below is a sample list of isps. Each line contains two to four fields
    # separated by a ','.
    Northrop Grumman Corp., United States
    Thomson Reuters (legal), United States
    Time Warner Cable, United States
    Tw Telecom Holdings, United States
    Windstream Business, United States
    Raytheon Company, Arizona, United States
    Intel Corporation, California, United States
    Rcn, New York, United States
    Peak 10, South Carolina, United States
    Charter Communications, Birmingham, Alabama, United States
    Google, Mountain View, California, United States
    Comcast Cable, Brewster, New York, United States
    Optimum Online, Bronx, New York, United States
    Choopa Llc, Miami, Florida, United States
    Quadranet, Miami, Florida, United States

We want to get the distribution of isp entries by the number of fields. That is we want to know how many lines have just two fields, how many have three and how many have four etc.,


    % cat isp.txt | grep -v "^$" | grep -v '^#' | awk '{print NF}' FS=,  | sort | uniq -c
          5 2
          4 3
          6 4

which shows that there 5 lines with two fields, 4 lines with 3 fields, 6 lines with 4 fields.

How the command works

grep -v "^$" - remvoes empty lines
grep -v '^#' - removes lines starting with '#'
awk '{print NF}' prints the number of fields in each line
  FS=, specifies the field separator
uniq prints the unique values in a sorted vector
  -c shows the count of each unique value.

Related links