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

后端 未结 13 688
情深已故
情深已故 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:54

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

    0 讨论(0)
  • 2020-12-23 04:00
    head -1 file.tsv |head -1 train.tsv |tr '\t' '\n' |wc -l
    

    take the first line, change tabs (or you can use ',' instead of '\t' for commas), count the number of lines.

    0 讨论(0)
  • 2020-12-23 04:05
    awk 'BEGIN{FS=","}END{print "COLUMN NO: "NF " ROWS NO: "NR}' file
    

    You can use any delimiter as field separator and can find numbers of ROWS and columns

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

    If counting number of columns in the first is enough, try the following:

    awk -F'\t' '{print NF; exit}' myBigFile.tsv

    where \t is column delimiter.

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

    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)
提交回复
热议问题