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