Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CSV dimensions from terminal

Suppose I'm in a folder where ls returns Test.csv. What command do I enter to get the number of rows and columns of Test.csv (a standard comma separated file)?

like image 378
user2763361 Avatar asked Nov 02 '13 10:11

user2763361


People also ask

How do I find the length of a CSV file?

Using len() function Under this method, we need to read the CSV file using pandas library and then use the len() function with the imported CSV file, which will return an int value of a number of lines/rows present in the CSV file.

How do I count lines in a CSV file in Linux?

The wc command is used to count the number of words or lines (with the -l option) in a file. In a CSV file, each line is a data record. Counting the number of rows is an easy way to have the number of records in your CSV file.

How create CSV file in Terminal?

To create a CSV file with a text editor, first choose your favorite text editor, such as Notepad or vim, and open a new file. Then enter the text data you want the file to contain, separating each value with a comma and each row with a new line.


1 Answers

Try using awk. It's best suited for well formatted csv file manipulations.

awk -F, 'END {printf "Number of Rows : %s\nNumber of Columns = %s\n", NR, NF}' Test.csv

-F, specifies , as a field separator in csv file.

At the end of file traversal, NR and NF have values of number of rows and columns respectively


Another quick and dirty approach would be like

# Number of Rows
cat Test.csv | wc -l

# Number of Columns
head -1 Test.csv | sed 's/,/\t/g' | wc -w
like image 71
jkshah Avatar answered Oct 16 '22 01:10

jkshah