-
-
Save lmlsna/ca45573160980dfb73d347696a6ddfa5 to your computer and use it in GitHub Desktop.
Bash script for a better version of the `uniq` shell command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# uniq - The default version of the uniq command only finds uniques that are adjacent | |
# and has no flag to find non-adjacent lines... so pretty useless. This is a | |
# bash script that actually does what you would expect uniq to do. | |
# for every piped in line: | |
for w in $(cat); do | |
# if we haven't already added it before | |
if [[ "$(grep -o "$w" <<< "$once")" == "" ]]; then | |
# add it + newline | |
once="$once$w\n" | |
fi | |
done | |
# print everything except the last empty newline | |
echo -e $once | head -n-1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment