Why do duplicate lines survive uniq? Imagine a monitor that records five observations of the same job: queued, running, running, queued, done. You pass the log through uniq and get “queued” twice. Both entries belong there. The second one tells you the job went back into the queue.
In this guide

Linux uniq collapses consecutive duplicate lines. To get one row per value across a whole file, use LC_ALL=C sort -u statuses.txt. If the order of first appearance matters, use the awk example below. Try the five-line example first, then choose a command for your own file.
Watch what happens to five lines
Run this example as written. The pipe sends the five status entries directly to uniq, so you can try it without creating a file.
printf '%s\n' queued running running queued done | uniq -c
Expected output, with the count padding omitted for readability:
1 queued
2 running
1 queued
1 done
Read each row as a stretch of time. The two adjacent “running” entries form one stretch. When one of the duplicate lines appears again, the earlier stretch has already ended. Collapsing only adjacent duplicate lines is exactly what you want when investigating a retry.
Choose which duplicate lines to keep
In an empty practice directory, create the sample file once. Run the remaining file examples in that directory. The first command writes five lines to statuses.txt.
printf '%s\n' queued running running queued done > statuses.txt
| What you need | Command | Output order |
|---|---|---|
| One copy of every value, sorted | LC_ALL=C sort -u statuses.txt | done, queued, running |
| Only values seen once in the entire file | LC_ALL=C sort statuses.txt | LC_ALL=C uniq -u | done |
| One copy in first-seen order | awk '!seen[$0]++' statuses.txt | queued, running, done |
The output column lists successive lines. The commas are there to make the table readable. LC_ALL=C gives the sort a predictable byte order for this lab.
Count each status across the whole file
For a frequency report, group duplicate lines before counting them. Try the same sequence again with a sort in the middle:
LC_ALL=C sort statuses.txt | LC_ALL=C uniq -c
1 done
2 queued
2 running
The counts add up to five, and the output has three rows because the input contains three distinct statuses. Sorting discards the chronology. Keep the original sequence if someone will need to investigate when the job returned to the queue.
Keep the first occurrence in its original position
Suppose a dashboard needs a list of statuses in the order a reader first encountered them. Sorting would put “done” at the top. Use a seen-set in awk to remember which lines have appeared:
awk '!seen[$0]++' statuses.txt
queued
running
done

$0 is the complete input line. The first time a line appears, its previous count is zero, so the condition succeeds and awk prints the line. On later visits the previous count is positive and the line is skipped. This approach keeps an entry in memory for every distinct line, which matters for a large file with millions of different values.
Find only the values that occur once
There are two plausible meanings of “unique.” A status picker needs one copy of every value. An audit looking for one-off events needs only values that appeared exactly once. For plain text with one status per line, choose the command that matches the report:
- Every distinct status:
LC_ALL=C sort -u statuses.txtreturns done, queued and running for this sample. - Only statuses seen once:
LC_ALL=C sort statuses.txt | LC_ALL=C uniq -ureturns done. - Only statuses seen more than once:
LC_ALL=C sort statuses.txt | LC_ALL=C uniq -dreturns queued and running, once each.

Running uniq -u directly on the original sequence would print “queued” twice. Each occurrence formed a one-line group in that order. Sorting puts all duplicate lines into groups before the singleton test runs.
Diagnose duplicate lines that look identical
Inspect a small sample before changing any data. On GNU/Linux, the following command displays the first 20 lines with an end marker and makes characters such as a carriage return visible:
LC_ALL=C sed -n '1,20l' statuses.txt
To reproduce the problem without modifying the sample file, send three versions of the same word through GNU sed. The second has a trailing space. The third has a carriage return.
printf 'queued\nqueued \nqueued\r\n' | LC_ALL=C sed -n lqueued$
queued $
queued\r$
For duplicate lines copied from different systems, read the end markers first. If one row displays queued$ and another displays queued $, the second contains a trailing space. A row ending in \r$ contains a carriage return before the newline. The dollar sign is a display marker. Check whether those differences are meaningful before trimming them. Likewise, if each row starts with a timestamp, decide whether your report should compare the whole event or just the status field.
Count the status field in a timestamped log
A real monitor often adds a timestamp to every observation. Two records can describe the same state while containing different text. Start by deciding what counts as one observation in your report. In the example below, each record has exactly two whitespace-separated fields. The first is the time and the second is a one-word status.
printf '%s\n' '09:00 queued' '09:01 running' '09:02 running' '09:03 queued' '09:04 done' | awk '{print $2}' | LC_ALL=C sort | LC_ALL=C uniq -cThe output should match the frequency report above. Try removing the awk stage. Each timestamp makes its line distinct, so every count becomes one. Both calculations are valid. One counts observations of each state, while the other counts identical full records. If a line includes a job ID as well, filter for the job you are investigating before interpreting the sequence as a history of that job.
The way you identify duplicate lines depends on the record format. A CSV field can contain a quoted comma and a status message can contain spaces. Check a few records and use a parser for that format before applying these line-based commands. In particular, a quoted CSV record can span multiple physical lines.
Verify the report before using the counts
A frequency table gives you two numbers to check independently. The number of output rows tells you how many distinct values appeared. The sum of the counts tells you how many observations went into the report. For the practice file, expect three values and five observations. Run this cross-check on the file you created earlier:
LC_ALL=C sort statuses.txt | LC_ALL=C uniq -c | awk '{total += $1} END {print NR, total}'The result is 3 5. If duplicate lines seem to have produced four distinct values, inspect spaces and line endings. If the total itself is wrong, check the input and any filtering stage before changing the grouping rule. For example, a blank line is a record too. Whether to count it depends on what an empty status means in the system that produced the file.
For a quick prediction exercise, imagine the monitor records one more “running” after “done.” The frequency report would still have three rows and its counts would sum to six. The run report would gain a fifth row, because the new observation begins a fresh run. Working out both answers before running the pipeline is a useful check on which report you actually need.
Why the job ID changes the answer
Imagine two workers reporting to the same log. One job is queued while another is already running. When both jobs later report “done,” their status fields match. If your question is “Which states appeared today?”, collapsing those duplicate lines after extracting the status is appropriate. If your question is “How many jobs reached done?”, keep the identifier until you have counted each job once.
printf '%s\n' 'A queued' 'B running' 'A done' 'A done' 'B done' | awk '$2 == "done" {print $1}' | LC_ALL=C sort -uThe output contains A and B. There are three completion observations, but only two distinct jobs with a completion observation. The extra A might be a repeated notification or a second observation of the same state. The log alone does not tell you which. Removing duplicate lines is a reporting choice here. Keep the original events if you need to investigate the cause.
Now change the question to “Which job sent repeated completion observations?” Run the same pipeline through sort | uniq -d in place of sort -u. Only A should appear. Filtering for the state before grouping by identifier matters: grouping every state together would also count the earlier queued or running observation for each job.
When sharing the result, name the unit in the report. “Two jobs reached done” and “three done observations” are both defensible summaries of the five lines. “Two duplicates” leaves the reader guessing which field was compared and whether the count refers to rows or jobs.
When duplicate lines come from two files
Suppose yesterday’s status list ends with “queued” and today’s list begins with “queued.” Joining the two files into one stream makes those observations adjacent. A run count across the joined stream will merge them. That is appropriate for a continuous timeline, provided the files are in chronological order and the last record in the first file ends with a newline. For a daily report, run the count on each day separately so the date boundary remains visible.
For an inventory across both days, pass both filenames to sort. For example, with files named monday.txt and tuesday.txt, run LC_ALL=C sort -u monday.txt tuesday.txt. Sorting collects duplicate lines from either day and writes the inventory to the terminal. To compare daily frequencies later, retain each original file. A combined inventory cannot tell you on which day a value occurred.
Decide whether case is significant
A monitor might switch from “queued” to “QUEUED” after a format change. These are different byte sequences. For a case-sensitive inventory that distinction can reveal the change. For a report on operational states you might intentionally group them. GNU uniq offers a case-insensitive comparison with -i. When looking for duplicate lines anywhere in a file, use a matching case-insensitive sort so equivalent spellings are adjacent:
printf '%s\n' queued RUNNING QUEUED | LC_ALL=C sort -f | LC_ALL=C uniq -icExpect one group with a count of two for the queue state and one group with a count of one for the running state. The spelling printed for a group comes from its first sorted entry. If you require a particular spelling in a published report, choose a display convention explicitly. This example uses ASCII status labels. A different language or a case-sensitive identifier may need a different comparison rule.
Should blank observations count?
A blank line between two matching entries interrupts the run. To see it, print “queued,” an empty line, and “queued” again:
printf '%s\n' queued '' queued | uniq -cThe middle output row has a count but no label. It records an empty observation. If the input specification defines empty lines as separators, you can remove just those lines before grouping with awk 'length($0) > 0'. A line containing a space still has a positive length. That distinction matters when a broken exporter writes spaces for missing values. Decide what a missing status means before treating visually blank entries as duplicate lines.
Check your result. How many lines would uniq -c statuses.txt print if the second “queued” entry moved next to the first? Three. The two queue entries would now form one run. The counts after sorting would stay the same. If counting lines in your source file produces a surprise of its own, see why wc -l can report one fewer line.
For the full comparison and grouping options, see the GNU Coreutils manual for uniq.
Count runs of duplicate lines in a sequence
A shift supervisor wants to know how long a sensor stayed blue before and after a gold reading. Try the five cards shown in the featured image.
printf 'blue\nblue\ngold\nblue\nblue\n' | uniq -c
printf 'blue\nblue\ngold\nblue\nblue\n' | LC_ALL=C sort | uniq -cCounting adjacent duplicate lines with the first command prints three runs in order: 2 blue, 1 gold, 2 blue. The second prints two totals: 4 blue and 1 gold. Both account for all five readings. Only the first output preserves the return to blue. If the report needs both duration in readings and total frequency, run each pipeline against the original sequence. Sorting the only copy discards the ordering needed to reconstruct those runs.