Sorting and Deduplicating Text with Bash sort and uniq

Repeated log entries grouped, sorted, and transformed into a frequency report

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 uniq and sort -u.
  • Count how many times each log entry appears.
  • Combine commands with a pipeline to produce a useful report.
Ad

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 -nr

Here is what each part does:

  • sort application.log places identical lines next to one another.
  • uniq -c combines identical neighboring lines and adds a count.
  • sort -nr sorts 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 -nr

Expected Output

      3 2026-08-18 ERROR database connection failed
      2 2026-08-18 INFO request completed
      1 2026-08-18 WARN response time exceeded

How the Code Works

A top-to-bottom Bash pipeline showing application log entries being sorted, grouped and counted with uniq, then numerically reverse-sorted into a frequency report with the most frequent entries first.
Sort log lines before uniq so repeated entries become adjacent; count each group, then rank counts from highest to lowest.

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:

  1. sort application.log: The lines are arranged alphabetically. This places identical log messages beside each other.
  2. uniq -c: The -c option means “count.” Instead of simply removing duplicates, uniq prints each different line with the number of consecutive occurrences.
  3. sort -nr: The -n option sorts numbers numerically, and -r reverses 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.log

Unlike 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 uniq without sorting first: Non-adjacent duplicates may not be combined. Use sort file | uniq or sort file | uniq -c.
  • Sorting counts as text: Plain sort may place 10 before 2 because it compares characters. Use sort -n for numeric values.
  • Forgetting reverse order: sort -n puts the smallest count first. Add -r when you want the most frequent entries at the top.
  • Assuming uniq changes 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 delayed

Your 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, and sort -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 -nr

The 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 retry

Key Takeaways

  • sort arranges lines so that identical entries are next to one another.
  • uniq removes adjacent duplicate lines, and uniq -c counts them.
  • Sort input before using uniq when duplicates may appear in different parts of a file.
  • Use sort -nr to order a frequency report from the largest count to the smallest.
  • Use sort -u when you only need a sorted list of unique lines.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad