Deduplicate Lines in Order With awk
Drop repeated lines but keep the first occurrence where it appeared.
Run awk '!seen[$0]++' file to print each unique line once, in order.
01Command
Command
$ awk '!seen[$0]++' file.txt02What it does
sort -u reorders data and uniq only removes adjacent duplicates, but this awk idiom remembers every line seen and prints the first occurrence in place. That order preservation matters for logs and generated lists, and it is trivial to script.
03Risk
!Warning: The whole file's unique lines are held in memory; extremely high-cardinality inputs need a database or sort instead.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- gawk.1.en.html manual page (Debian bookworm)Evidence: Documents associative arrays, used here to remember seen lines.
Last verified: 2026-10-13 · Category: Developer