Sorting CSV file by first column, ignoring header
sort
does not have an option to exclude header. You can remove the header using:
tail -n+2 yourfile | sort
This tail syntax gets yourfile
from second line up to the end of the file.
Of course the results of sort
will not include the header.
You can isolate the header with command head -n1 yourfile
which will print only the first line of your file (your header).
To combine them together you can run:
head -n1 yourfile && tail -n+2 yourfile | sort
I assume you want to keep the header: redirect the contents of the file into a grouped construct:
{
# grab the header and print it untouched
IFS= read -r header
echo "$header"
# now process the rest of the input
sort
} < file.csv
Using csvkit
:
$ csvsort -c 1 file.csv
or just
$ csvsort file.csv
The difference is that the first command will only use the first column, while the second will use all (like sort
does).
The csvkit
tools assumes that the input CSV file has a header line as its first line. Use -H
if the file has no header.