sed trouble with exact pattern match

  • Follow


Hello,
I have a list of trial names like so:
$ list="test1 test2 test test3 test4"

I need to delete a word from a string once a trial is completed for
further work.
Say, say I need to delete the word 'test', I get:
$ echo $list | sed s/test1//g
1 2  3 4

Which is not what I expected. I have enclosed the pattern with quotes,
but
that does not make a difference.

I need to end up with:
test1 test2 test3 test4

Any  pointers please

thanks
Pete.
0
Reply peter 5/26/2010 12:11:19 PM

On May 26, 1:11=A0pm, peter sands <peter_sa...@techemail.com> wrote:
> Hello,
> I have a list of trial names like so:
> $ list=3D"test1 test2 test test3 test4"
>
> I need to delete a word from a string once a trial is completed for
> further work.
> Say, say I need to delete the word 'test', I get:
> $ echo $list | sed s/test1//g
> 1 2 =A03 4
>
> Which is not what I expected. I have enclosed the pattern with quotes,
> but
> that does not make a difference.
>
> I need to end up with:
> test1 test2 test3 test4
>
> Any =A0pointers please
>
> thanks
> Pete.

yes, what an idiot I am, just drop the g
0
Reply peter 5/26/2010 12:17:49 PM


peter sands wrote:

> Hello,
> I have a list of trial names like so:
> $ list="test1 test2 test test3 test4"
> 
> I need to delete a word from a string once a trial is completed for
> further work.
> Say, say I need to delete the word 'test', I get:
> $ echo $list | sed s/test1//g
> 1 2  3 4
> 
> I need to end up with:
> test1 test2 test3 test4
> 
> Any  pointers please
> 
> thanks
> Pete.

Try using names which are not subsets of other names. Failing that:

list=$(echo $list | tr ' ' '\n' | grep -v '^test$' | tr '\n' ' ')
0
Reply Andrew 5/26/2010 12:59:42 PM

On May 26, 7:17=A0am, peter sands <peter_sa...@techemail.com> wrote:
> On May 26, 1:11=A0pm, peter sands <peter_sa...@techemail.com> wrote:
>
>
>
>
>
> > Hello,
> > I have a list of trial names like so:
> > $ list=3D"test1 test2 test test3 test4"
>
> > I need to delete a word from a string once a trial is completed for
> > further work.
> > Say, say I need to delete the word 'test', I get:
> > $ echo $list | sed s/test1//g
> > 1 2 =A03 4
>
> > Which is not what I expected. I have enclosed the pattern with quotes,
> > but
> > that does not make a difference.
>
> > I need to end up with:
> > test1 test2 test3 test4
>
> > Any =A0pointers please
>
> > thanks
> > Pete.
>
> yes, what an idiot I am, just drop the g- Hide quoted text -
>
> - Show quoted text -

Presumably you've tried that now and discovered that won't work it'll
just leave you with a "1" as the first field.

Some versions of sed support word delimiters \< and \>:

$ echo "test1 test test2" | sed 's/test//'
1 test test2
$ echo "test1 test test2" | sed 's/\<test\>//'
test1  test2

See if that works on your sed. If you want to get rid of the white
space after "test" too so you don't end up double-spaced between the
surrounding words:

$ echo "test1 test test2" | sed 's/\<test\>[[:space:]]*//'
test1 test2

     Ed.
0
Reply Ed 5/26/2010 2:21:23 PM

In news:704396ae-66b5-482b-a897-3cae4eca1c14@z17g2000vbd.googlegroups.com,
peter sands <peter_sands@techemail.com> typed:

> I have a list of trial names like so:
> $ list="test1 test2 test test3 test4"
>
> I need to delete a word from a string once a trial is completed for
> further work.
> Say, say I need to delete the word 'test', I get:
> $ echo $list | sed s/test1//g
> 1 2  3 4
>
> Which is not what I expected. I have enclosed the pattern with quotes,
> but that does not make a difference.

You need to quote more than just the pattern space.

> I need to end up with:
> test1 test2 test3 test4
>
> Any  pointers please

$ echo $list | sed 's/test1//'
 test2 test test3 test4


0
Reply Greg 5/26/2010 2:55:36 PM

Yes you're right , taking the 'g' off does not work,
Using the tr method as posted , appears to do the job, though still
testing.

thanks
Pete.

0
Reply peter 5/27/2010 6:17:02 AM

[Please quote context.]
[Please reply to the posting with the solution you are referring to.]

peter sands wrote:
> Yes you're right , taking the 'g' off does not work,
> Using the tr method as posted , appears to do the job, though still
> testing.

Assuming you are referring to:

  list=$(echo $list | tr ' ' '\n' | grep -v '^test$' | tr '\n' ' ')

Mind that this command involves four(!) processes, which is a lot of
overhead compared to a plain shell solution.

On what platform are you working, what shells have you available?

If you have any modern shell (ksh93, bash, etc.) you can do

  list="test1 test2 test test3 test4"
  list=" $list "
  printf "%s\n" "${list/ test / }"

to obtain

  test1 test2 test3 test4

You may add the spaces to the data list (if it is under your control)
to simplify the code

  list=" test1 test2 test test3 test4 "
  printf "%s\n" "${list/ test / }"


Janis

> 
> thanks
> Pete.
> 
0
Reply Janis 5/27/2010 8:24:04 AM

6 Replies
1214 Views

(page loaded in 0.081 seconds)

Similiar Articles:













7/22/2012 12:54:01 PM


Reply: