Bash Tutorial · Pipes and Text Processing · Lesson 12 of 30

Bash sort, uniq, cut and tr

Sort lines, remove duplicates, extract columns and translate characters.

Create a free account to save this page, take notes and track your progress. Sign in

The toolbox

  • sort sorts lines: -n numeric, -r reverse, -k2 by the second field, -t, with a comma separator, -u unique.
  • uniq removes adjacent duplicates (sort first); uniq -c counts them.
  • cut -d, -f2 extracts the second comma-separated field; cut -c1-5 the first five characters.
  • tr 'a-z' 'A-Z' translates characters; tr -d '\r' deletes them (handy for Windows line endings).
Counting, sorting and extracting
cat > visits.txt <<'EOF'
/home
/orders
/home
/cart
/home
/orders
EOF
sort visits.txt | uniq -c | sort -rn
echo ---
printf 'ana,DE,31\nben,US,25\nchen,DE,40\n' > people.csv
sort -t, -k3 -n people.csv | cut -d, -f1
echo "make me loud" | tr 'a-z' 'A-Z'

Output

      3 /home
      2 /orders
      1 /cart
---
ben
ana
chen
MAKE ME LOUD

Run this on your computer — see “Get Started” for setup.

Test yourself

Why do you usually run sort before uniq?

  1. uniq can't read files
  2. uniq only removes duplicates that are next to each other
  3. sort removes duplicates itself
  4. It's faster
Show answer

Answer: uniq only removes duplicates that are next to each other

Was this page helpful?