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

后端 未结 13 720
情深已故
情深已故 2020-12-23 03:09

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:57

    Perl solution:

    perl -ane '$maxc = $#F if $#F > $maxc; END{$maxc++; print "max columns: $maxc\nrows: $.\n"}' file

    If your input file is comma-separated:

    perl -F, -ane '$maxc = $#F if $#F > $maxc; END{$maxc++; print "max columns: $maxc\nrows: $.\n"}' file

    output:

    max columns: 5
    rows: 2
    

    -a autosplits input line to @F array
    $#F is the number of columns -1
    -F, field separator of , instead of whitespace
    $. is the line number (number of rows)

提交回复
热议问题