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

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

    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.

提交回复
热议问题