You are not logged in.
Pages: 1
In a bash script I have two lists of variables, lets call them A and B. B is a superset of A, i.e. contains all elements of A plus some extras. I am trying to reduce B to just the values not in A.
So far, I have got:
for i in $A
do
B=`echo $B | sed "s# $i # #"`
done
The spaces are to ensure whole words are matched. I.e. removing "foo" from "foobar foo etc" works properly. The problem is that this misses the first and last values in B because they don't start/finish with a space.
I tried
B=`echo $B | sed "s#[ \^]$i[ \$]# #"`
but that did not make any difference...
I have the feeling I am so close! Any suggestions. Thanks.
Offline
You could try to use word matches. Using
\< and \>
I just tried this, see if it helps you:
--> AA="hi you how are you"
--> BB="hi to what are fine"
--> for word in $AA
> do
> BB=$(echo $BB | sed 's#\<'$word'\>##')
> done
--> echo $BB
to what fine
Offline
Thanks, that worked. So many different sed methods to learn...
Offline
I can recommend the chapters on Regex and Sed from The Grymoire.
Offline
More sed confusion
Can anybody tell me what is going on here... Removing trailing spaces:
sed "s/[ ]*$//" <file> - works
sed 's/[ ]*$//' <file> - works
sed "s#[ ]*$##" <file> - fails
sed 's#[ ]*$##' <file> - works
sed "s#[ ]*$###" <file> - runs (don't know if works though)
Annoying, because I naturally use double quotes (allows variable substitution) and hash as the delimiter...
Last edited by Allan (2007-10-19 16:27:58)
Offline
Try doing echo instead of sed to see what bash thinks of your script.
You'll see the $# is also a variable that gets substituted
And you can turn off the single quote to sneak in a bash variable.
Offline
Pages: 1