sort, uniq, wc and cut: work with lines and columns
Text files with one record per line, such as logs, lists and CSV files, can be sorted, counted and summarised with a few small commands. They never change the original file. They print a result, which you can view or send to a new file.
wc: count
asha@pc:~/demo$ wc notes.txt
3 9 51 notes.txt
asha@pc:~/demo$ wc -l marks.csv
6 marks.csv
asha@pc:~/demo$ wc -w notes.txt
9 notes.txtWith no option, wc prints lines, words and bytes. -l counts lines, -w words and -c bytes. Given several files, it totals them.
asha@pc:~/demo$ wc -l *.txt
8 log.txt
3 notes.txt
5 report.txt
16 totalsort
asha@pc:~/demo$ sort notes.txt
Buy milk
Call the plumber
Pay the electricity billAlphabetical by default. The useful options:
| Option | Effect |
|---|---|
-r | reverse |
-n | sort as numbers |
-h | understand sizes such as 2K and 1G |
-u | drop duplicate lines |
-k 2 | sort by the second field |
-t , | fields are separated by commas |
-f | ignore case |
Numbers need -n. Without it they sort as text, where 100 comes before 9.
asha@pc:~/demo$ printf "9\n100\n25\n" | sort
100
25
9
asha@pc:~/demo$ printf "9\n100\n25\n" | sort -n
9
25
100Sorting by a column
-t names the separator and -k the field. This sorts the marks file by the second column, as numbers, highest first. tail -n +2 leaves out the heading row.
asha@pc:~/demo$ tail -n +2 marks.csv | sort -t , -k 2 -n -r
Meena,92
Asha,78
Tara,67
Ravi,45
Kabir,33Add | head -n 3 for the top three.
uniq: repeated lines
uniq removes lines that repeat the line before. It only compares neighbours, so the input must be sorted first.
asha@pc:~/demo$ cut -c7-11 log.txt | uniq
INFO
ERROR
INFO
ERROR
WARN
INFO
asha@pc:~/demo$ cut -c7-11 log.txt | sort | uniq
ERROR
INFO
WARN The first result still has repeats, because equal lines were not next to each other. After sorting, each value appears once.
uniq -c counts how many times each line occurred. Followed by a numeric sort, it gives a frequency table, one of the most used recipes on the command line.
asha@pc:~/demo$ cut -c7-11 log.txt | sort | uniq -c | sort -rn
5 INFO
2 ERROR
1 WARN uniq -d shows only the lines that are repeated, and uniq -u only those that are not.
cut: columns
cut takes part of each line. -d gives the separator and -f the fields wanted.
asha@pc:~/demo$ cut -d , -f 1 marks.csv
name
Asha
Ravi
Meena
Kabir
Tara
asha@pc:~/demo$ cut -d , -f 2 marks.csv
marks
78
45
92
33
67-f 1,3 takes fields 1 and 3, and -f 2- everything from field 2 onwards. -c cuts by character position instead.
asha@pc:~/demo$ cut -c1-5 log.txt
09:00
09:02
09:05
09:06
09:07
09:10
09:15
09:16cut cannot cope with separators that vary, such as several spaces. For that, awk is the tool: awk '{print $2}' prints the second word of each line, whatever the spacing.
asha@pc:~/demo$ awk '{print $2}' log.txt | sort | uniq -c
2 ERROR
5 INFO
1 WARNtr: change characters
tr replaces or deletes characters in what it reads.
asha@pc:~/demo$ echo "hello world" | tr a-z A-Z
HELLO WORLD
asha@pc:~/demo$ echo "a,b,c" | tr , "\n"
a
b
c
asha@pc:~/demo$ echo "2026-11-12" | tr -d -
20261112In order: small letters to capitals, commas to line breaks, and dashes deleted.
Adding up a column
There is no "sum" command, but awk adds a column in one line.
asha@pc:~/demo$ tail -n +2 marks.csv | awk -F , '{total += $2} END {print total}'
315-F , sets the separator. For each line the second field is added to a running total, and the total is printed at the end.
Putting them together
How many different words are in a file?
asha@pc:~/demo$ tr -s " " "\n" < notes.txt | sort -u | wc -l
8tr turns each space into a line break, giving one word per line. sort -u removes duplicates, and wc -l counts.
The highest mark.
asha@pc:~/demo$ tail -n +2 marks.csv | cut -d , -f 2 | sort -n | tail -n 1
92Saving the result
These commands print. To keep the result, send it to a different file.
asha@pc:~/demo$ sort notes.txt > sorted.txt
asha@pc:~/demo$ cat sorted.txt
Buy milk
Call the plumber
Pay the electricity billDanger. sort file.txt > file.txt wipes the file. The shell empties it before sort reads it. Write to a new name, or use sort -o file.txt file.txt, which is made for sorting a file in place.
CSV files with awkward data
cut and sort split on every comma. Real CSV files sometimes have commas inside quoted values, such as "Verma, Asha". These tools will split that in the wrong place. For such files use a spreadsheet, or a tool that understands CSV properly.
Other small tools
| Command | Does |
|---|---|
nl | number the lines |
tac | print the lines in reverse order (Linux) |
rev | reverse each line |
paste | join files side by side |
column -t | line text up in columns |
diff a b | show the differences between two files |
On Windows, sort exists and find /c counts lines. See type and findstr.
Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.
This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.