Unix Shell - Searching for a string and print next string

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Searching for a string and print next string





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Searching for a string and print next string
apogeusistemas@gmail.com

2007-05-25, 1:24 am

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.

Janis Papanagnou

2007-05-25, 1:24 am

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
Ed Morton

2007-05-25, 1:24 am

Janis Papanagnou wrote:
> apogeusistemas@gmail.com wrote:
>
>
> 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

William James

2007-05-25, 7:17 am

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

Chris F.A. Johnson

2007-05-26, 7:21 am

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
Ed Morton

2007-05-26, 1:24 pm

Chris F.A. Johnson wrote:
> On 2007-05-25, apogeusistemas@gmail.com wrote:
>
>
>
> awk 'sub( /.*pineapple /,"") { print $1 }' file1
>
>


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

2007-05-26, 1:24 pm

On Sat, 26 May 2007 07:32:32 -0500, Ed Morton
<morton@lsupcaemnt.com> wrote:
>
>
> Chris F.A. Johnson wrote:
>
> 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?
John W. Krahn

2007-05-27, 7:18 pm

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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com