I want to delete duplicate strings from a String. Example:
A="Dog Cat Horse Dog Dog Cat"
The string A should look like this:
A="Dog Cat Horse"
How can I write a Shell script for that?
You could use this,
echo "a a b b c c" | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed -e 's/[[:space:]]*$//'
If order is not important, you can use an associative array:
declare -A uniq
for k in $A ; do uniq[$k]=1 ; done
echo ${!uniq[@]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With