DEV Community

BC
BC

Posted on • Edited on

6 1

Shell commands for simple tasks of processing CSV file - Linux Tips

Paste

Use paste to concat 2 csv files:

$ cat students.csv
Name,Grad Year
Alice,2024
Bob,2022
Chris,2023

$ cat score.csv
Name,GPA
Alice,B+
Bob,A
Chris,A-

# concat 2 csv files
$ paste -d "," students.csv score.csv
Name,Grad Year,Name,GPA
Alice,2024,Alice,B+
Bob,2022,Bob,A
Chris,2023,Chris,A-
Enter fullscreen mode Exit fullscreen mode

Cut

Use cut to get designated columnes:

# Use -f 1,2,4 to get only 1st, 2nd and 4th column
$ paste -d "," students.csv score.csv | cut -d , -f 1,2,4
Name,Grad Year,GPA
Alice,2024,B+
Bob,2022,A
Chris,2023,A-
Enter fullscreen mode Exit fullscreen mode

Sort

Use sort to sort specified field in CSV file:

$ cat students.csv
Alice,85,alice@example.com
Bob,92,bob@example.com
Chris,60,chris@example.com

# now sort by score in descending order
# -t: split by
# -k: field number after split
# -n: compare field as number, not string
# -r: in reverse order
$ sort -t , -k 2 -nr students.csv
Bob,92,bob@example.com
Alice,85,alice@example.com
Chris,60,chris@example.com
Enter fullscreen mode Exit fullscreen mode

Awk

Use awk to get fields in CSV file:

$ cat students.csv
Alice,85,alice@example.com
Bob,92,alice@example.com
Chris,60,alice@example.com

# use `awk` to split each line and get 2nd field
$ cat students.csv | awk -F, '{print $2}'
85
92
60

# pipe with `sort` to get the highest score
$ cat students.csv | awk -F, '{print $2}' | sort | tail -1
92
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay