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