csvkit can be used to extract and process fields from a csv file on Linux or Mac.
- Install python3 if not installed already. The command to install python3 on Amazon Linux
sudo yum install python3
- For Mac you can use the following command
brew install python3
- Install csvkit
pip3 install csvkit - Extract 2nd field
printf "first,second,third\nfirst2,second2,third2\n" | csvcut -c 2
second
second2 - Extract 2nd field and use delimiter space
printf "first second third\nfirst2 second2 third2\n" | csvcut -d " " -c 2
second
second2 - Extract column by name
printf "col1name,col2name,col3name\nfirst,second,third\nfirst2,second2,third2\n" | csvcut -c col1name
col1name
first
first2 - Sort on first field
printf "col1name,col2name,col3name\nc,c1,c2\nb,b1,b2\n" | csvsort -c 1
col1name,col2name,col3name
b,b1,b2
c,c1,c2 - Grep data where 2nd field field has specific value
printf "col1name,col2name,col3name\nc,c1,c2\nb,b1,b2\n" | csvgrep -c 2 -r 'b1'
col1name,col2name,col3name
b,b1,b2
- Ignore header (this will create default header a,b,c,… in output)
printf "col1name,col2name,col3name\nc,c1,c2\nb,b1,b2\n" | csvgrep -H -c 2 -r 'b1'
a,b,c
b,b1,b2