Skip to content

Columns and characters: cut and tr

IntermediateLesson 21 of 667 min

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.csv
cut -d, -f2 orders.csv
cut -d, -f2,4 orders.csv

Without -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 -c

tr: 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-Z
tr , ";" < orders.csv

tr -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.

Next