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 > Searching for a string and print next string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Searching for a string and print next string

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2007, 01h35   #1
apogeusistemas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Searching for a string and print next string

Please, how can I find a specified string and print the next string ?
I need find pineapple string and print next string in the line.
Ex:
Solaris> cat file1
The strawberry and the pineapple are delicious.
Solaris> ./script
are

Thanks.

  Réponse avec citation
Vieux 25/05/2007, 01h49   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

apogeusistemas@gmail.com wrote:
> Please, how can I find a specified string and print the next string ?
> I need find pineapple string and print next string in the line.
> Ex:
> Solaris> cat file1
> The strawberry and the pineapple are delicious.
> Solaris> ./script
> are
>
> Thanks.
>


Maybe one of...

awk '{for(f=1;f<=NF;f++)if($f~/pineapple/)print $(f+1)}'

awk -v RS=\ 't{print;exit}/pineapple/{t=1}'


Janis
  Réponse avec citation
Vieux 25/05/2007, 03h41   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

Janis Papanagnou wrote:
> apogeusistemas@gmail.com wrote:
>
>> Please, how can I find a specified string and print the next string ?
>> I need find pineapple string and print next string in the line.
>> Ex:
>> Solaris> cat file1
>> The strawberry and the pineapple are delicious.
>> Solaris> ./script
>> are
>>
>> Thanks.
>>

>
> Maybe one of...
>
> awk '{for(f=1;f<=NF;f++)if($f~/pineapple/)print $(f+1)}'


I'd go with:

awk '{for(f=1;f<=NF;f++)if($f~/^pineapple$/)print $(f+1)}'
or:
awk '{for(f=1;f<=NF;f++)if($f=="pineapple")print $(f+1)}'

to make sure you don't match on "pineapple" when you're looking for "apple".

Note that the above will print a blank line if "pineapple" is the last
word on the line, which may or may not be a problem...

Ed.

> awk -v RS=\ 't{print;exit}/pineapple/{t=1}'
>
>
> Janis

  Réponse avec citation
Vieux 25/05/2007, 03h49   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

On 2007-05-25, apogeusistemas@gmail.com wrote:
> Please, how can I find a specified string and print the next string ?
> I need find pineapple string and print next string in the line.
> Ex:
> Solaris> cat file1
> The strawberry and the pineapple are delicious.
> Solaris> ./script
> are


awk 'sub( /.*pineapple /,"") { print $1 }' file1


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 25/05/2007, 09h10   #5
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

On May 24, 7:35 pm, apogeusiste...@gmail.com wrote:
> Please, how can I find a specified string and print the next string ?
> I need find pineapple string and print next string in the line.
> Ex:
> Solaris> cat file1
> The strawberry and the pineapple are delicious.
> Solaris> ./script
> are
>
> Thanks.


ruby -ne 'puts $1 if /\bpineapple\s+(\S+)/' file1

  Réponse avec citation
Vieux 26/05/2007, 13h32   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

Chris F.A. Johnson wrote:
> On 2007-05-25, apogeusistemas@gmail.com wrote:
>
>>Please, how can I find a specified string and print the next string ?
>>I need find pineapple string and print next string in the line.
>>Ex:
>>Solaris> cat file1
>>The strawberry and the pineapple are delicious.
>>Solaris> ./script
>>are

>
>
> awk 'sub( /.*pineapple /,"") { print $1 }' file1
>
>


What if you were looking for "apple" and "pineapple" was in the input file?
  Réponse avec citation
Vieux 26/05/2007, 14h34   #7
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

On Sat, 26 May 2007 07:32:32 -0500, Ed Morton
<morton@lsupcaemnt.com> wrote:
>
>
> Chris F.A. Johnson wrote:
>> On 2007-05-25, apogeusistemas@gmail.com wrote:
>>
>>>Please, how can I find a specified string and print the next string ?
>>>I need find pineapple string and print next string in the line.
>>>Ex:
>>>Solaris> cat file1
>>>The strawberry and the pineapple are delicious.
>>>Solaris> ./script
>>>are

>>
>>
>> awk 'sub( /.*pineapple /,"") { print $1 }' file1
>>
>>

>
> What if you were looking for "apple" and "pineapple" was in the input file?


You can use \< and \> to match word boundaries.


--
If life is merely a joke, the question still remains: for whose amusement?
  Réponse avec citation
Vieux 27/05/2007, 19h49   #8
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Searching for a string and print next string

apogeusistemas@gmail.com wrote:
> Please, how can I find a specified string and print the next string ?
> I need find pineapple string and print next string in the line.
> Ex:
> Solaris> cat file1
> The strawberry and the pineapple are delicious.
> Solaris> ./script
> are


$ echo "The strawberry and the pineapple are delicious." | \
perl -lne'print /pineapple\s+(\S+)/'
are



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
  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 19h19.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14579 seconds with 16 queries