~$ skillshelf
← OverTheWire Bandit

Bandit 8 → 9: the line that only happens once

banditlinuxshell

Every line in the file looks like the password. Nothing about the line tells you which one it is — the answer is a property of the file as a whole.

the goal

The password for the next level is stored in the file data.txt and is the only line of text that occurs only once.

the approach

bandit8@bandit:~$ ls
data.txt

A thousand-odd lines, all the same shape: random-looking strings, one per line. Looking at them gets you nowhere. What separates the real one is that every other line appears more than once and this one doesn’t.

I told the AI to write me a loop that finds the unrepeated line:

bandit8@bandit:~$ while read -r line; do
>   if [ "$(grep -cx "$line" data.txt)" -eq 1 ]; then
>     echo "$line"
>   fi
> done < data.txt

Read the file line by line; for each line, count how many times that exact line appears in the whole file; print it if the count is 1. grep -c counts matches instead of printing them, and -x forces the match to be the whole line rather than a substring.

It works, and it printed one line: the password.

It’s also slower than it looks. That loop re-reads the entire file once per line — a thousand lines means a thousand passes, a million line comparisons for a job that should take one pass. On this file you don’t notice. On a big one you would.

the takeaway

The one-pass version of this is two commands:

bandit8@bandit:~$ sort data.txt | uniq -u

uniq only ever compares adjacent lines — that’s the thing to remember, and it’s why sort has to come first. Unsorted, duplicates scattered through the file never sit next to each other and uniq sees nothing. Sorted, they’re all neighbours.

Then the flag picks what you want out of the grouping:

flag gives you
uniq -u only the lines that appear once — this level
uniq -d only the lines that appear more than once
uniq -c every line with a count in front of it

sort | uniq -c | sort -rn — count everything, most frequent first — is the one you’ll actually reuse. It’s how you find the noisiest IP in a log file.