OverTheWire Bandit: Level 8 → Level 9
Context
In Linux, we can use the output of one command as the input for another. This is called Piping, represented by the vertical bar |. This feature is the heart of the Unix philosophy: “Write programs that do one thing and do it well. Write programs to work together.”
In this level, we have a data.txt file containing thousands of lines of text. However, only one line occurs exactly once; all others are repeated.
Goal
Find the single unique line (the password) in data.txt.
Solution
To filter out duplicate lines, we use the uniq command. However, uniq has a catch: It only detects duplicates if they are adjacent (next to each other).
Since our file is scrambled, uniq alone won’t work effectively. We must first verify the data is sorted alphabetically (sort) so that identical lines sit together, and then apply uniq.
This is where the Pipe (|) comes in:
sort data.txt: Sort the file content.|: Take the sorted output…uniq -u: …and feed it intouniq. (The-uflag stands for “unique”, printing only lines that appear once).
combining the commands:
sort data.txt | uniq -u
Output:
EN632PlfYiZbn3PhVK3XOGSlNInNE00t
Bingo! Thousands of duplicate lines are filtered out, leaving only the unique password.
Key Takeaways
- Piping (
|): Allows chaining commands. The output of the left command becomes the input of the right command. sort: Sorts data alphabetically or numerically.uniq: Filters duplicate lines. Almost always used aftersort. The-uflag shows only unique lines, while-c(count) counts occurrences.