Columns and characters: cut and tr
Pull a column out of a CSV with cut -d, -f, count it with sort and uniq -c, and change case or delete characters with tr. Practise in your browser.
A lot of everyday data comes in columns: spreadsheets saved as CSV, logs, lists of users. Two small tools pull it apart.
cut: keep some columns
cut splits every line at a delimiter and prints the fields you ask for. -d names the delimiter (a comma for CSV) and -f the field numbers:
head -n 3 orders.csvcut -d, -f2 orders.csvcut -d, -f2,4 orders.csvWithout -d, cut splits at tabs, so a CSV comes out whole. -c cuts by character position instead: cut -c1-10 keeps the first ten.
The first line of a CSV is usually a header. tail -n +2 prints everything from line 2 on, which drops it. That makes a handy pipeline with the counting trick from the Beginner level:
cut -d, -f2 orders.csv | tail -n +2 | sort | uniq -ctr: change characters
tr (translate) swaps characters one for one: each one in the first set becomes the one in the same place in the second.
echo "Hello World" | tr a-z A-Ztr , ";" < orders.csvtr -d deletes characters and tr -s " " squeezes runs of spaces into one. tr only reads its input, never a file name: send a file in with < or a pipe.
Your turn
Count the orders from each city in orders.csv: print each city with its number of orders, without the header line. Then save a lower-case copy of emails.txt as emails-clean.txt.
Your task
Count the orders from each city in orders.csv: print each city with its number of orders, without the header line. Then save a lower-case copy of emails.txt as emails-clean.txt.
- Each city with its number of orders (not done yet)
- emails-clean.txt is emails.txt in lower case (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
cut -d- Fields are separated by DELIM (one character) instead of a tab More on cut
cut -f- Print these fields (columns) More on cut
tail -n +2- Everything from line 2 on: drops a header line More on tail
tr- Translate or delete characters More on tr
Every everyday command on one page: the Linux commands cheat sheet.