|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
I want to write a shell script that modifies a string - string1, so that the last occurence of another string - string2 and anything after that remains in string1. I believe I can use sed but cant figure out how. e.g: s1="string1 string2 string3 string2 and more strings" s2="string2" output will be: s1="string2 and more strings" -Kay |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
2006-11-2, 10:45(-08), Kay:
> Hi > I want to write a shell script that modifies a string - string1, so > that the last occurence of another string - string2 and anything after > that remains in string1. I believe I can use sed but cant figure out > how. > e.g: > s1="string1 string2 string3 string2 and more strings" > s2="string2" > output will be: > s1="string2 and more strings" [...] case $s1 in *"$s2"*) s1=$s2${s1#*"$2"};; esac -- Stéphane |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Kay wrote:
> Hi > I want to write a shell script that modifies a string - string1, so > that the last occurence of another string - string2 and anything after > that remains in string1. I believe I can use sed but cant figure out > how. > e.g: > s1="string1 string2 string3 string2 and more strings" > s2="string2" > output will be: > s1="string2 and more strings" > > -Kay > If the strings contain no "/"s, s1=$(echo "$s1" | sed "s/.*$s2/$s2/") -- Regards, ---Robert |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Robert Katz wrote:
> Kay wrote: >> Hi >> I want to write a shell script that modifies a string - string1, so >> that the last occurence of another string - string2 and anything after >> that remains in string1. I believe I can use sed but cant figure out >> how. >> e.g: >> s1="string1 string2 string3 string2 and more strings" >> s2="string2" >> output will be: >> s1="string2 and more strings" >> >> -Kay >> > > If the strings contain no "/"s, > > > s1=$(echo "$s1" | sed "s/.*$s2/$s2/") > I guess some punctuation charaters, like (") would cause trouble too. -- Regards, ---Robert |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2006-11-02, 20:39(+00), Robert Katz:
> Kay wrote: >> Hi >> I want to write a shell script that modifies a string - string1, so >> that the last occurence of another string - string2 and anything after >> that remains in string1. I believe I can use sed but cant figure out >> how. >> e.g: >> s1="string1 string2 string3 string2 and more strings" >> s2="string2" >> output will be: >> s1="string2 and more strings" >> >> -Kay >> > > If the strings contain no "/"s, And no . \ * $ [ > s1=$(echo "$s1" | sed "s/.*$s2/$s2/") Note that it selects the rightmost $s2. -- Stéphane |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Stephane CHAZELAS wrote:
> 2006-11-02, 20:39(+00), Robert Katz: >> Kay wrote: >>> Hi >>> I want to write a shell script that modifies a string - string1, so >>> that the last occurence of another string - string2 and anything after >>> that remains in string1. I believe I can use sed but cant figure out >>> how. >>> e.g: >>> s1="string1 string2 string3 string2 and more strings" >>> s2="string2" >>> output will be: >>> s1="string2 and more strings" >>> >>> -Kay >>> >> If the strings contain no "/"s, > > And no . \ * $ [ (") would cause trouble too. > >> s1=$(echo "$s1" | sed "s/.*$s2/$s2/") > > Note that it selects the rightmost $s2. That's what she wanted (the rightmost $s2). -- Regards, ---Robert |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Kay wrote:
> Hi > I want to write a shell script that modifies a string - string1, so > that the last occurence of another string - string2 and anything after > that remains in string1. I believe I can use sed but cant figure out > how. > e.g: > s1="string1 string2 string3 string2 and more strings" > s2="string2" > output will be: > s1="string2 and more strings" > > -Kay > For old shells I recommend expr: s1=`expr x"$s1" : x".*\($s2.*\)"` -- Michael Tosch @ hp : com |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
2006-11-02, 21:21(+00), Robert Katz:
> Stephane CHAZELAS wrote: >> 2006-11-02, 20:39(+00), Robert Katz: >>> Kay wrote: >>>> Hi >>>> I want to write a shell script that modifies a string - string1, so >>>> that the last occurence of another string - string2 and anything after >>>> that remains in string1. I believe I can use sed but cant figure out >>>> how. >>>> e.g: >>>> s1="string1 string2 string3 string2 and more strings" >>>> s2="string2" >>>> output will be: >>>> s1="string2 and more strings" >>>> >>>> -Kay >>>> >>> If the strings contain no "/"s, >> >> And no . \ * $ [ > > (") would cause trouble too. Why?? >>> s1=$(echo "$s1" | sed "s/.*$s2/$s2/") >> >> Note that it selects the rightmost $s2. > > That's what she wanted (the rightmost $s2). Then expr "x$s1" : "x.*\($s2.*\)" should do (with same restrictions on the characters in $s2, and $s1 must contain $s2). -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|