Reputation: 31
Given these two strings:
"12345"
"1245"
Where the first one is the complete string and the second has things missing from the first, I want it to return "3".
So again with:
"The ball is red"
"The is red"
I want to to return "ball"
I've tried diff with:
diff <(echo "12345") <(echo "1245")
But diff isn't giving the desired output. comm doesn't do what I want either.
Upvotes: 3
Views: 6323
Reputation: 360113
Using only one external executable:
a='The ball is red'
b='The is red'
join -v 1 <(echo "${a// /$'\n'}") <(echo "${b// /$'\n'}")
Using join
and grep
on strings with no spaces:
a=12345
b=1245
join -v 1 <(echo "$a" | grep -o .) <(echo "$b" | grep -o .)
Upvotes: 1
Reputation: 49812
I think that comm
is the correct command:
comm -23 <(tr ' ' $'\n' <<< 'The ball is red') <(tr ' ' $'\n' <<< 'The is red')
or more flexible:
split_spaces() { tr ' ' $'\n' <<< "$1"; }
split_chars() { sed $'s/./&\\\n/g' <<< "$1"; }
comm -23 <(split_spaces 'The ball is red') <(split_spaces 'The is red')
comm -23 <(split_chars 12345) <(split_chars 1245)
Upvotes: 5