Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Created November 18, 2017 00:17
Show Gist options
  • Save lmlsna/ca45573160980dfb73d347696a6ddfa5 to your computer and use it in GitHub Desktop.
Save lmlsna/ca45573160980dfb73d347696a6ddfa5 to your computer and use it in GitHub Desktop.
Bash script for a better version of the `uniq` shell command
#!/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