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: gawk problem matching multiple patterns ?!? HOW-TO? - comp.lang ...... lines issue AWK/SED Multiple pattern matching over multiple lines ... files which filter different patterns. If you have a gawk-doc ... pattern matching problem in sed ... Join two lines in pattern matching, search, then assign fields to ...The problem is that the database is dumped with 2 lines ... LINENUM + 1` LINE2=`lynx -dump $URL | cat -n | sed ... Join two lines in pattern matching, search, then ... sed, remove last new line - comp.lang.awk... closed, here is the problem: I have to use "sed ... when it prints the pattern space. The latest version of GNU sed ... sed replace string when line match something - comp ... extracting a particular pattern from a line - comp.unix.shell ...Can this be done with sed, awk or perl ... digitis and/or letters - if that's a problem, let us know. Also, if you want to match multiple occurrences of each pattern on ... Grep string with spaces - comp.unix.shell... grep $ALIASIP`" = "" ] The problem is that with this, 192.168.100.1 will match 192 ... space at the end) to ensure an exact match ... Make the pattern simpler, or try to free ... sed: printing only between {} - comp.unix.shell... cfaj.freeshell.org> Shell Scripting Recipes: A Problem ... Jackson Brooklyn How would I print only lines match ... only specific lines based on the line number or pattern ... sed - what do these lines do ? - comp.unix.shell... joy of reverse engineering the real problem ... sed replace string when line match something - comp.unix.shell ... sed and ... all lines that don't contain the pattern. Sed can ... ANSI terminal escape sequence regexp - comp.unix.shell... character would do, so for example with GNU sed's ERE to match ... It gives a more restrictive pattern: (\x1b ... RegExp problem: - comp.lang.javascript ANSI terminal ... Generating all possible combinations of a 5 digit pattern. - comp ...... of frames is a laborious task at the least and the big problem with pattern matching is that ... defined list of possible patterns, until the program finds an exact match ... How to use grep function to search exact word in HP UNIX 11.0 ...... having located the lines that contain the pattern ... linker symbol binding problem - comp.unix.solaris I did ... tried to use grep function in HP UNIX 11.0 for matching exact ... [SOLVED] Remove exact matching word using sed - LinuxQuestions.orgHaving a problem logging in? Please visit this ... Remove exact matching word using sed ... matching a pattern in a file & prefixing a word SED: match pattern & delete matched lines - The UNIX and Linux ForumsI need to use SED to match anything with a ... two files using awk. | Problem with ... SED: delete and print the only exact matched pattern 7/22/2012 12:54:01 PM
|