How do I count the number of rows and columns in a file using bash?

后端 未结 13 1122
深忆病人
深忆病人 2020-12-23 03:07

Say I have a large file with many rows and many columns. I\'d like to find out how many rows and columns I have using bash.

相关标签:
13条回答
  • 2020-12-23 03:55

    Columns: awk '{print NF}' file | sort -nu | tail -n 1

    Use head -n 1 for lowest column count, tail -n 1 for highest column count.

    Rows: cat file | wc -l or wc -l < file for the UUOC crowd.

    0 讨论(0)
  • 2020-12-23 03:56

    Alternatively to count columns, count the separators between columns. I find this to be a good balance of brevity and ease to remember. Of course, this won't work if your data include the column separator.

    head -n1 myfile.txt | grep -o " " | wc -l
    

    Uses head -n1 to grab the first line of the file. Uses grep -o to to count all the spaces, and output each space found on a new line. Uses wc -l to count the number of lines.

    0 讨论(0)
  • 2020-12-23 03:57

    Simple row count is $(wc -l "$file"). Use $(wc -lL "$file") to show both the number of lines and the number of characters in the longest line.

    0 讨论(0)
  • 2020-12-23 03:58

    For rows you can simply use wc -l file

    -l stands for total line

    for columns uou can simply use head -1 file | tr ";" "\n" | wc -l

    Explanation
    head -1 file
    Grabbing the first line of your file, which should be the headers, and sending to it to the next cmd through the pipe
    | tr ";" "\n"

    tr stands for translate.
    It will translate all ; characters into a newline character.
    In this example ; is your delimiter.

    Then it sends data to next command.

    wc -l
    Counts the total number of lines.

    0 讨论(0)
  • 2020-12-23 04:02

    Following code will do the job and will allow you to specify field delimiter. This is especially useful for files containing more than 20k lines.

    awk 'BEGIN { 
      FS="|"; 
      min=10000; 
    }
    { 
      if( NF > max ) max = NF; 
      if( NF < min ) min = NF;
    } 
    END { 
      print "Max=" max; 
      print "Min=" min; 
    } ' myPipeDelimitedFile.dat
    
    0 讨论(0)
  • 2020-12-23 04:06

    A very simple way to count the columns of the first line in pure bash (no awk, perl, or other languages):

    read -r line < $input_file
    ncols=`echo $line | wc -w`
    

    This will work if your data are formatted appropriately.

    0 讨论(0)
提交回复
热议问题