0

I have two strings as below-

token1, token2, token3, token4, token5, token6, token8, token9, token10

token2, token7, token4, token3, token5, token6, token8, token10, token9

Visually, I can see that "tokens" like token1 and token7 are not present in both strings. But is there an easy way to get the differing tokens using Unix tools?

The long route would be to write a script and maintain a hashmap of {token => count} and in the end print only those keys that have count = 1. But I suppose there is a shorter way.

2

GNUly:

s1='token1, token2, token3, token4, token5, token6, token8, token9, token10'
s2='token2, token7, token4, token3, token5, token6, token8, token10, token9'
comm <(grep -oE '\w+' <<< "$s1" | sort) <(grep -oE '\w+' <<< "$s2" | sort)

Gives:

token1
                token10
                token2
                token3
                token4
                token5
                token6
        token7
                token8
                token9

The columns are:

  1. tokens only in s1
  2. tokens only in s2
  3. tokens in both.

You suppress a column by passing the corresponding option (like -3 to suppress the 3rd column).

1

Pinching the basic idea from Ramesh

With GNU awk in bash

awk -v RS='[[:space:]]*,[[:space:]]*' '{x[$0]++}; END{for (y in x) if (x[y] == 1) print y}'  
<(printf "%s" 'token1, token2, token3, token4, token5, token6, token8, token9, token10')  
<(printf "%s" 'token2, token7, token4, token3, token5, token6, token8, token10, token9')
token1
token7
  • I still have to make it print only the different values :) Trying to find out how to do it :) – Ramesh Mar 27 '14 at 21:25
1

You can do something like below.

cat input1 input2 >> output
arr=$(cat output | tr "," "\n")
echo "${arr[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '

Explanation

I am merging the 2 files together into another file and splitting the tokens with comma as a delimiter. After that I am printing only the values that are unique (meaning only the values that occur once which I believe is what you are looking for).

input1 file contents

token1, token2, token3, token4, token5, token6, token8, token9, token10

input2 file contents

token2, token7, token4, token3, token5, token6, token8, token10, token9

After I execute the above script, I get the output as,

token1 token10 token2 token3 token4 token5 token6 token7 token8 token9

If you observe the above output, it would print only the unique values from both the files.

If you however need the difference alone, you can use the below command.

echo ${arr[@]} | sort | uniq -c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.