PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > finding a string in another
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

finding a string in another

Réponse
 
LinkBack Outils de la discussion
Vieux 02/11/2006, 18h45   #1
Kay
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut finding a string in another

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

  Réponse avec citation
Vieux 02/11/2006, 18h58   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 20h39   #3
Robert Katz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 20h40   #4
Robert Katz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 21h03   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 21h21   #6
Robert Katz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 21h21   #7
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Vieux 02/11/2006, 21h23   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding a string in another

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14476 seconds with 16 queries