sort, uniq, cut, tr and wc

Beginner · 8 min

Five small filters that turn raw text into answers: sort by field or number, count duplicates with uniq -c, slice with cut, rewrite with tr, count with wc.

Before thisPipes and pipelinesSearching with grep

tutoriaLinux · 8 min

sort, uniq, cut, tr and wc

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by tutoriaLinuxReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • sort orders lines as text by default, so 10 comes before 9. Add -n for numbers, -r to reverse, -t and -k to sort on a field, -u to drop duplicates.
  • uniq only collapses adjacent duplicates, so sort first. uniq -c prefixes each line with its count, and sort | uniq -c | sort -rn is the classic frequency table.
  • cut -d, -f1,4 picks delimited fields and cut -c1-4 picks characters. It always outputs fields in file order, and it cannot treat runs of spaces as one separator.
  • tr translates or deletes single characters from stdin only: tr a-z A-Z, tr -d '\r', tr -s ' '. It takes no file name.
  • wc -l counts newline characters, so a last line without a newline is not counted. BSD wc pads its numbers and wc -m depends on the locale.

One job each

The previous lessons got you a stream of lines. Real questions sound like “which status codes appear, and how often?” Answering one means picking a column, ordering, collapsing duplicates, and counting. Each of those is a tiny program that does one thing, and the pipe joins them. This lesson covers the five you will use most. All examples ran on macOS with the web-log and people-CSV fixture.

sort

By default sort compares lines as text, character by character:

$ printf '10\n9\n100\n2\n' | sort | tr '\n' ' '
10 100 2 9
$ printf '10\n9\n100\n2\n' | sort -n | tr '\n' ' '
2 9 10 100
$ printf '10\n9\n100\n2\n' | sort -nr | tr '\n' ' '
100 10 9 2

1 sorts before 2, so 100 precedes 2. Use -n for numbers and -r to reverse. To sort a CSV by its fourth column, name the delimiter and the key. -k4,4 limits the key to field 4 (plain -k4 means “field 4 to the end of the line”):

$ tail -n +2 data/people.csv | sort -t, -k4,4nr | head -3
margaret,flight,boston,97
grace,compilers,new york,95
ada,infra,london,91

I dropped the header with tail -n +2 first. In an earlier run without that, sort -n put name,team,city,score on top, since it has no number and counts as zero. -u removes duplicates (b a b became a b). BSD sort also accepted -h (human sizes: 500 1K 2M) and -V (version order: v1.2 before v1.10), which I ran. Case order depends on the locale. My environment gave A B a b, and -f folded case into A a B b. Set LC_ALL=C in scripts when you need byte order.

uniq

uniq removes adjacent repeats, nothing more:

$ printf 'a\nb\na\n' | uniq | tr '\n' ' '
a b a
$ printf 'a\nb\na\n' | sort | uniq | tr '\n' ' '
a b
$ printf 'a\nb\na\nc\n' | sort | uniq -c
   2 a
   1 b
   1 c

-c adds counts, -d prints only lines that repeat and -u only lines that don’t. The pattern you will type a hundred times is sort | uniq -c | sort -rn:

$ cut -d' ' -f8 logs/access.log | sort | uniq -c | sort -rn
 104 200
  24 500
  24 304
  16 404
  16 403
  16 301

That is a frequency table of the log’s status codes: 104 successes, 24 server errors and so on.

cut

cut picks pieces of each line. -d sets the delimiter and -f the fields. -c picks character positions:

$ cut -d, -f1,4 data/people.csv | head -3
name,score
ada,91
linus,88
$ cut -c1-4 data/people.csv | head -3
name
ada,
linu
$ cut -d, -f4,1 data/people.csv | head -2
name,score
ada,91

The last one asked for fields 4 then 1 and got 1 then 4: cut keeps file order. It also has no notion of “runs of whitespace”:

$ printf 'a   b\n' | cut -d' ' -f2

$ printf 'a   b\n' | tr -s ' ' | cut -d' ' -f2
b

The first printed an empty line, since the second field lies between two of the three spaces. Squeezing first fixes it, and awk '{print $2}' (two lessons ahead) handles it natively. The default delimiter is a tab, and a line with no delimiter is printed whole: printf 'a,b\n' | cut -f2 gave a,b. GNU’s --complement doesn’t exist here (cut: illegal option -- -).

tr

tr translates or deletes characters. It reads only stdin, so give it input with < or a pipe. Passing a file name just prints a usage message:

$ echo 'hello World' | tr a-z A-Z
HELLO WORLD
$ echo 'hello' | tr -d l
heo
$ echo 'a   b    c' | tr -s ' '
a b c
$ printf 'a,b\n' | tr ',' '\n'
a
b

-d deletes, -s squeezes repeats into one. Classes such as [:upper:] work too. tr -d '\r' strips Windows carriage returns, and od -c showed plain \n endings afterwards. Beware that tr works on characters, one for one. It does not replace words. When the second set is shorter, BSD tr pads it with its last character: echo abcdef | tr abc x gave xxxdef.

wc

wc counts. -l lines, -w words, -c bytes, -m characters:

$ wc data/people.csv
       6       8     145 data/people.csv
$ printf 'no newline' | wc -l
       0

-l counts newline characters, so text with no trailing newline reports 0. BSD wc pads with spaces (use $(wc -l < file) or tr -d ' ' if you need the bare number). With héllo, wc -c said 7 bytes and wc -m said 7 in my default locale and 6 once I set LC_ALL=en_US.UTF-8: character counts depend on the locale.

Putting it together, unique clients in the log:

$ cut -d' ' -f1 logs/access.log | sort -u | wc -l
       5

About the video

tutoriaLinux’s “Linux Sysadmin Basics 4.1 – Filtering Output and Finding Things (&&, cut, sort, uniq, wc, grep)” was published 22 March 2014 and runs 8 minutes 28 seconds. Its description lists &&, cut (select one of two fields), sort, uniq, wc and the basics of grep, so it demonstrably covers four of this lesson’s five commands. It has no chapters, so the whole video is embedded, including the && and grep parts that belong to other lessons. I couldn’t read a transcript. No single video I found covers tr along with the others, so the tr section here is my own runs. Learn Linux TV’s 13-minute “Linux Crash Course - The tr Command” is linked under deeper to fill that gap; I judged it by its title only. English per the title and description, though the channel has many auto-dub tracks. It is the oldest video in the group, and the description promotes a Udemy course. These commands haven’t changed. Dev Portal’s 2026 video on sort and uniq (chapters at 0:34 sorting, 4:16 uniq, 8:20 a grep | sort | uniq -c example, and a description that names sort -n, uniq -c, -d and -u) is the second view. It replaced a 2020 Hackpens log-filtering video with a one-line description.

What I did not run

GNU sort, cut, tr and wc. join, paste beyond one use, and sort on very large inputs with -S and -T.

Next

cut and tr can’t edit text in place. For substitutions across lines, use editing streams with sed.

Check yourself

  1. 01sort puts 100 before 9. How do you get numeric order?

    Use sort -n. Without it, sort compares text character by character, and the character 1 sorts before 9.

  2. 02Why does printf 'a\nb\na\n' | uniq still show a twice?

    uniq only merges adjacent identical lines. Sort first so equal lines are next to each other, giving sort | uniq or sort -u.

  3. 03You run cut -d' ' -f2 on 'a b' and get an empty line. Why, and what works instead?

    cut treats every single space as a separator, so there are empty fields between the three spaces. Squeeze first with tr -s ' ' | cut -d' ' -f2, or use awk '{print $2}'.

  4. 04How do you turn Windows line endings into Unix ones?

    tr -d '\r' < in.txt > out.txt removes the carriage returns. I checked that it leaves lines ending in a plain newline.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark