Intermediate · 7 min · practise every command in the built-in terminal

cut and tr

cut — Extract columns

cut -d: -f1 /etc/passwd        # Field 1, delimiter :
cut -d: -f1,3 /etc/passwd      # Fields 1 and 3
cut -d, -f2 data.csv           # CSV second column
cut -c1-10 file.txt            # Characters 1-10
cut -c5- file.txt              # From character 5 to end

tr — Translate characters

echo 'hello' | tr 'a-z' 'A-Z'     # Lowercase to uppercase
echo 'Hello World' | tr ' ' '_'   # Replace spaces with _
echo 'hello' | tr -d 'l'          # Delete all 'l' chars
cat file.txt | tr -s ' '          # Squeeze multiple spaces

Practical examples

# Extract all usernames from /etc/passwd
cut -d: -f1 /etc/passwd

# Get list of user shells
cut -d: -f7 /etc/passwd | sort | uniq -c

# Convert CSV column to uppercase
cut -d, -f1 data.csv | tr 'a-z' 'A-Z'

Practice tasks

  1. Extract only the usernames (first field) from /etc/passwd
  2. Convert the text 'hello world' to uppercase using tr
  3. Extract characters 1-10 from README.txt
  4. Delete every 'l' character from the word 'hello' using tr

Commands in this lesson

Text Processing