Bootstrap FreeKB - Bash (Scripting) - remove duplicate values from a variable
Bash (Scripting) - remove duplicate values from a variable

Updated:   |  Bash (Scripting) articles

Let's say you have a variable (foo in this example) with dupliate values (bar in this example).

foo="hello world bar bar bar bar bar"

 

The following command will remove the duplicate values from the variable (credit goes to this Stack Overflow post).

echo "$foo" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s",$i,FS)}{printf("\n")}'

 

The result will be:

hello world bar

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b43505 in the box below so that we can be sure you are a human.