What You’ll Learn
In this lesson, you’ll learn how to use Bash’s sort and uniq commands to organize repeated text and create a frequency report from application log entries.
- Sort log lines alphabetically or numerically.
- Remove duplicate lines with
uniqandsort -u. - Count how many times each log entry appears.
- Combine commands with a pipeline to produce a useful report.
The Concept
Log files often contain repeated messages. For example, an application might record the same database error several times. Reading every line individually makes it difficult to see which messages occur most often.
The Bash sort command arranges lines in order. The uniq command removes repeated adjacent lines or counts them. Because uniq only compares neighboring lines, you usually sort the input before sending it to uniq.
A common frequency-report pipeline is:
sort application.log | uniq -c | sort -nrHere is what each part does:
sort application.logplaces identical lines next to one another.uniq -ccombines identical neighboring lines and adds a count.sort -nrsorts the counts numerically in reverse order, putting the most frequent entries first.
This is useful for quickly finding repeated errors, busy endpoints, or common status messages without writing a larger script.
Basic Example
The following example creates a small application log and produces a frequency report. The cat command with <<'EOF' writes the sample lines into a file. In a real project, you could replace this setup step with an existing log file.
cat > application.log <<'EOF'
2026-08-18 ERROR database connection failed
2026-08-18 INFO request completed
2026-08-18 ERROR database connection failed
2026-08-18 WARN response time exceeded
2026-08-18 INFO request completed
2026-08-18 ERROR database connection failed
EOF
sort application.log | uniq -c | sort -nrExpected Output
3 2026-08-18 ERROR database connection failed
2 2026-08-18 INFO request completed
1 2026-08-18 WARN response time exceededHow the Code Works
The first command creates application.log with six log entries. Three entries are identical database errors, two are identical informational messages, and one is a warning.
The pipeline then processes the file from left to right:
sort application.log: The lines are arranged alphabetically. This places identical log messages beside each other.uniq -c: The-coption means “count.” Instead of simply removing duplicates,uniqprints each different line with the number of consecutive occurrences.sort -nr: The-noption sorts numbers numerically, and-rreverses the order. Together, they place the largest count first.
The spaces before each number are added by uniq -c. They do not affect the report’s meaning.
A key detail is that uniq does not search the entire file for duplicates. It only compares neighboring lines. For example, if the same message appears on lines 1 and 3 with a different message between them, uniq will treat those two occurrences as separate groups. Sorting first prevents this problem.
If a Bash script depends on external commands, you can use checking command availability with Bash command -v to verify that required tools are available before the script runs.
Another Example
Sometimes you do not need counts. You may only want a clean list of distinct log messages. The -u option of sort sorts the input and removes duplicate lines in one command.
cat > payment.log <<'EOF'
2026-08-18 INFO payment approved
2026-08-18 ERROR payment provider unavailable
2026-08-18 INFO payment approved
2026-08-18 WARN payment retry scheduled
2026-08-18 ERROR payment provider unavailable
EOF
sort -u payment.logUnlike the frequency report, this command displays each complete line only once. This is useful when you want to inspect the kinds of messages in a log without seeing repeated copies.
Common Mistakes
- Using
uniqwithout sorting first: Non-adjacent duplicates may not be combined. Usesort file | uniqorsort file | uniq -c. - Sorting counts as text: Plain
sortmay place 10 before 2 because it compares characters. Usesort -nfor numeric values. - Forgetting reverse order:
sort -nputs the smallest count first. Add-rwhen you want the most frequent entries at the top. - Assuming
uniqchanges the original file: These commands print a result but do not modify the input file. Use output redirection if you want to save the result.
Try It Yourself
Create a file named worker.log containing repeated messages from a background worker. Then use a pipeline to display each distinct line and its frequency, with the most frequent line first.
Use the following entries:
2026-08-18 INFO job completed
2026-08-18 ERROR job failed
2026-08-18 INFO job completed
2026-08-18 INFO job completed
2026-08-18 WARN job delayedYour command should use sort, uniq -c, and a numeric reverse sort.
Challenge
Create a Bash frequency report for the following checkout-service log entries:
- Save the entries in a file named
checkout.log. - Count each complete line.
- Display the most frequent line first.
- Use a pipeline containing
sort,uniq -c, andsort -nr.
Solution
cat > checkout.log <<'EOF'
2026-08-18 INFO checkout started
2026-08-18 ERROR inventory service unavailable
2026-08-18 INFO checkout started
2026-08-18 WARN payment retry
2026-08-18 ERROR inventory service unavailable
2026-08-18 INFO checkout started
2026-08-18 ERROR inventory service unavailable
EOF
sort checkout.log | uniq -c | sort -nrThe first command creates the input file. The pipeline sorts the complete log lines so duplicates are adjacent, counts each group, and then sorts the counts from highest to lowest.
Expected Output
3 2026-08-18 ERROR inventory service unavailable
3 2026-08-18 INFO checkout started
1 2026-08-18 WARN payment retryKey Takeaways
sortarranges lines so that identical entries are next to one another.uniqremoves adjacent duplicate lines, anduniq -ccounts them.- Sort input before using
uniqwhen duplicates may appear in different parts of a file. - Use
sort -nrto order a frequency report from the largest count to the smallest. - Use
sort -uwhen you only need a sorted list of unique lines.



