How to read CSVs from the command line
Passing spreadsheets around as CSVs is pretty common these days, and I'm frankly quite thankful for this. With cloud-based office tools now being the norm, and proprietary formats getting increasingly difficult to operate on with our greasy unix tools, it's such a relief to get hold of a good ol' fashioned text file that I can mangle in the terminal.
There is nothing magical about CSV files, or comma-separated value files. They are text files with specifically formatted contents, nothing more. As text files, you can treat them like any other text file.
Retrieve one or more rows from a CSV
Use the tail
and head
commands to chop off the top and bottom rows:
1tail -n+3 test.csv | head -n 2
tail -n+3
means "start at line 3", head -n 2
means "take just the first 2 lines".
Removing the column headers
Many CSVs include column headers on their first line
1name,age,city 2jon,32,oxford 3safi,42,london 4laura,28,manchester
This is primarily for human use, so when you're passing data from the CSV into other processes, you can skip this row using the tail
approach we mentioned just a moment ago.
1tail -n+1 test.csv
Retrieve one or more columns from a CSV
You don't need any special apps; macOS and Linux both come with the tools you need to extract this data without much difficulty - the cut
command is all you need
1tail -n+1 test.csv | cut -d , -f 1,3 2 3# => jon,oxford 4# safi,london 5# laura,manchester
cut
is used for extracting specific sections of each line of a file. In this case, we tell it to use a comma as a delimeter to mark the start of a new field (-d ,
) and return fields 1 and 3 (-f 1,3
)