|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|