- Open the Terminal. (on macOs, you can use Spotlight with keyboard shortcut ⌘ + Space)
- Move terminal working directory to the folder containing your CSV file. Use ls to show current directory files and folders and cd to move inside a folder.
- Enter the following command. Replace filepath with your CSV file path:
wc -l filepath.csv
- The line count will be printed before the file name
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.
Get the number of rows containing a keyword in a CSV file
If you want to filter rows in your CSV file and count the matching lines, combine wc with the command grep.
grep "keyword" filepath.csv | wc -l
You can also search for multiple keywords with grep using a regular expression (the -E option is for --extended-regexp). Any line with at least one of the keywords will be counted.
grep -E "keyword1|keyword2" filepath.csv | wc -l
By default, grep is case sensitive (uppercase and lowercase characters are distinct). To ignore case in the search, add the -i (for --ignore-case) in your command:
grep -i "keyword" filepath.csv | wc -l
or
grep -i -E "keyword1|keyword2" filepath.csv | wc -l
Get the number of rows of multiple CSV files
To have the sum of the rows of all the CSV files in the current folder, use the following command:
cat *.csv | wc -l
To view the content of your CSV file, you can uses an online CSV editor.