cut, sort -u & tail -n +2
Work with columns
Lesson 13 of 20 · 10 min · Module 4: Find and organize
CSV files and logs are tables made of text. A few commands let you pick, filter, and sort their columns.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Pick columns with cut
- Skip a header row
- Sort and remove duplicates
The idea
cut -d, -f1 splits each line at commas and keeps field 1. -f1,3 keeps fields 1 and 3. tail -n +2 starts at line 2, skipping a header row. sort -u sorts and removes duplicate lines in one step.
cut -d, -f1,2 data/servers.csv
tail -n +2 data/servers.csv | cut -d, -f3
01Choose the delimiter
-d sets the character between columns, here a comma. -f picks the fields by position, counting from 1.
cut -d, -f1,4 data/servers.csv | head -n 3name,city
web-01,Bucharest
web-02,Cluj02Filter rows, then pick columns
grep keeps the rows you want and cut keeps the columns you want. Together they behave like a tiny database query.
03Skip the header
tail -n +2 means "from line 2 to the end". Without it, the word role from the header would appear in your list of roles.
Your turn
data/servers.csv (columns: name, ip, role, city): 1) show the names of the servers in Bucharest, one per line; 2) show each role once, sorted alphabetically, without the header.What the checks look for
- Shows web-01, db-01 and web-03
- Shows cache, database, web once each without the header
Take this with you
grep picks rows, cut picks columns, sort -u keeps one of each.
Go a little further
Count how many servers each city has with cut, sort, and uniq -c.