Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

How do you de-duplicate lines while keeping the original order?

IntermediateConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
awk '!seen[$0]++' hosts.txt

seen[$0]++ is 0 (false) the first time a line appears, so ! makes it true and awk prints it; later occurrences are skipped. sort -u also de-duplicates but loses order. Use it for PATH-like lists: PATH=$(printf '%s' "$PATH" | awk -v RS=: -v ORS=: '!s[$0]++'); PATH=${PATH%:}.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq