You are not logged in.

#1 2007-10-17 13:23:53

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,385
Website

sed expression

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

#2 2007-10-17 14:02:33

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: sed expression

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

#3 2007-10-18 00:18:28

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,385
Website

Re: sed expression

Thanks, that worked. So many different sed methods to learn...

Offline

#4 2007-10-18 02:12:23

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: sed expression

I can recommend the chapters on Regex and Sed from The Grymoire.

Offline

#5 2007-10-19 16:24:52

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,385
Website

Re: sed expression

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

#6 2007-10-19 17:00:19

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: sed expression

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 wink

And you can turn off the single quote to sneak in a bash variable.

Offline

Board footer

Powered by FluxBB