From: peter sands on
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.
From: peter sands on
On May 26, 1:11 pm, peter sands <peter_sa...(a)techemail.com> 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
>
> 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.

yes, what an idiot I am, just drop the g
From: Andrew McDermott on
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' ' ')
From: Ed Morton on
On May 26, 7:17 am, peter sands <peter_sa...(a)techemail.com> wrote:
> On May 26, 1:11 pm, peter sands <peter_sa...(a)techemail.com> 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
>
> > 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.
>
> 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.
From: Greg Russell on
In news:704396ae-66b5-482b-a897-3cae4eca1c14(a)z17g2000vbd.googlegroups.com,
peter sands <peter_sands(a)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