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%:}.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner