Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix Command to get the count of lines in a csv file

Hi I am new to UNIX and I have to get the count of lines from incoming csv files. I have used the following command to get the count.

wc -l filename.csv 

Consider files coming with 1 record iam getting some files with * at the start and for those files if i issue the same command iam getting count as 0. Does * mean anything in here and if i get a file with ctrlm(CR) instead of NL how to get the count of lines in that file. gimme a command that solves the issue. Thanks in advance.

like image 504
Devoloper250 Avatar asked Mar 19 '14 16:03

Devoloper250


People also ask

How do I count the number of rows in a CSV file in Unix?

To count the number of records (or rows) in several CSV files the wc can used in conjunction with pipes. In the following example there are five CSV files. The requirement is to find out the sum of records in all five files. This can be achieved by piping the output of the cat command to wc.

How do I count the number of lines in 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 you count the number of lines in a file Unix?

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, but you can also use it to count lines in a file by adding the -l option. wc -l foo will count the number of lines in foo .

How do I count the number of lines in a file in Linux?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.


2 Answers

The following query helps you to get the count

cat FILE_NAME  | wc -l 
like image 86
Sathish Kumar Avatar answered Oct 05 '22 19:10

Sathish Kumar


All of the answers are wrong. CSV files accept line breaks in between quotes which should still be considered part of the same line. If you have either Python or PHP on your machine, you could be doing something like this:

Python

//From stdin cat *.csv | python -c "import csv; import sys; print(sum(1 for i in csv.reader(sys.stdin)))"  //From file name python -c "import csv; print(sum(1 for i in csv.reader(open('csv.csv'))))" 

PHP

//From stdin cat *.csv | php -r 'for($i=0; fgetcsv(STDIN); $i++);echo "$i\n";'  //From file name php -r 'for($i=0, $fp=fopen("csv.csv", "r"); fgetcsv($fp); $i++);echo "$i\n";' 

I have also created a script to simulate the output of wc -l: https://github.com/dhulke/CSVCount

like image 43
Danilo Souza Morães Avatar answered Oct 05 '22 21:10

Danilo Souza Morães