Unix Shell - Finding a string in a file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Finding a string in a file





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 Finding a string in a file
apogeusistemas@gmail.com

2007-05-28, 1:19 pm

Hi:
I need search a specified string in a file, after the word
"mounting" , like this:

solaris> cat file
netbackup, orac_ux001 backup, streaming yes, multiplexing ok ,
mounting M00541 police enabled, data file

Which awk command can I issue to extract only the "M00541" string
inside a file ?

Thanks

Franco

2007-05-28, 1:19 pm

On May 28, 2:52 pm, apogeusiste...@gmail.com wrote:
> Hi:
> I need search a specified string in a file, after the word
> "mounting" , like this:
>
> solaris> cat file
> netbackup, orac_ux001 backup, streaming yes, multiplexing ok ,
> mounting M00541 police enabled, data file
>
> Which awk command can I issue to extract only the "M00541" string
> inside a file ?
>
> Thanks


Why not use grep ?

grep -i mounting file ???????


This will extract all instances' of mounting and its associated line.

Ed Morton

2007-05-28, 1:19 pm

Franco wrote:
> On May 28, 2:52 pm, apogeusiste...@gmail.com wrote:
>
>
>
> Why not use grep ?
>
> grep -i mounting file ???????
>
>
> This will extract all instances' of mounting and its associated line.
>


He doesn't want the line or the word itself, he wants just the word
following it the word he's searching for.

For the OP - This will work for the sample input you posted:

awk '{for (i=1;i<=NF;i++) if ($i == "mounting") print $(i+1)}' file

but what would you want to have happen if "mounting" was the last word
on a line?

Ed.
tmp123

2007-05-28, 1:19 pm

On May 28, 3:52 pm, apogeusiste...@gmail.com wrote:
> Hi:
> I need search a specified string in a file, after the word
> "mounting" , like this:
>
> solaris> cat file
> netbackup, orac_ux001 backup, streaming yes, multiplexing ok ,
> mounting M00541 police enabled, data file
>
> Which awk command can I issue to extract only the "M00541" string
> inside a file ?
>
> Thanks


Could this statement be useful?

expr "`grep mounting file`" : 'mounting *\(\w*\)'

Kind regards.


apogeusistemas@gmail.com

2007-05-28, 7:19 pm

On 28 maio, 12:34, Ed Morton <mor...@lsupcaemnt.com> wrote:
> Franco wrote:
>
>
>
>
>
>
>
>
> He doesn't want the line or the word itself, he wants just the word
> following it the word he's searching for.
>
> For the OP - This will work for the sample input you posted:
>
> awk '{for (i=3D1;i<=3DNF;i++) if ($i =3D=3D "mounting") print $(i+1)}' fi=

le
>
> but what would you want to have happen if "mounting" was the last word
> on a line?
>
> Ed.- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -


Is there any way to limit the substring founded in 6 characters ?
I=B4m getting this:

M00541,05/28/07

Thank you !

Janis Papanagnou

2007-05-28, 7:19 pm

apogeusistemas@gmail.com wrote:
> On 28 maio, 12:34, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> Is there any way to limit the substring founded in 6 characters ?
> I´m getting this:


You wouldn't get that with your explanation and with the example data
you gave above. To limit the string to six characters replace/extend
the print command by

print substr($(i+1),1,6)


Janis

>
> M00541,05/28/07
>
> Thank you !
>

Bill Marcum

2007-05-28, 7:19 pm

On 28 May 2007 13:57:44 -0700, apogeusistemas@gmail.com
<apogeusistemas@gmail.com> wrote:
>
>
> On 28 maio, 12:34, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
> Is there any way to limit the substring founded in 6 characters ?
> I´m getting this:
>
> M00541,05/28/07
>
> Thank you !
>

print substr($(i+1),1,6)
sub(/,.*/,"",$(i+1)); print $(i+1)

--
Recent investments will yield a slight profit.
apogeusistemas@gmail.com

2007-05-29, 1:22 pm

On 28 maio, 18:37, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> apogeusiste...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
file[vbcol=seagreen]
>
>
>
>
>
> You wouldn't get that with your explanation and with the example data
> you gave above. To limit the string to six characters replace/extend
> the print command by
>
> print substr($(i+1),1,6)
>
> Janis
>
>
>
>
>
>
>
> - Mostrar texto entre aspas -- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -


Thank you and sorry for my wrong example !

Ed Morton

2007-05-29, 1:22 pm

apogeusistemas@gmail.com wrote:
> On 28 maio, 18:37, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
<snip>[vbcol=seagreen]
<snip>[vbcol=seagreen]
<snip>[vbcol=seagreen]
<snip>[vbcol=seagreen]
> Thank you and sorry for my wrong example !
>


If you posted an accurate example, I suspect there's an even simpler
solution.

Ed.
apogeusistemas@gmail.com

2007-05-29, 7:21 pm

On 29 maio, 11:31, Ed Morton <mor...@lsupcaemnt.com> wrote:
> apogeusiste...@gmail.com wrote:
>
>
>
>
>
>
>
> <snip>
' file[vbcol=seagreen]
> <snip>
>
> <snip>
> <snip>
>
> If you posted an accurate example, I suspect there's an even simpler
> solution.
>
> Ed.- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -


How could I substitute string "mounting" for "media id" in this
script ?

Ed Morton

2007-05-29, 7:21 pm

apogeusistemas@gmail.com wrote:
> On 29 maio, 11:31, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> How could I substitute string "mounting" for "media id" in this
> script ?
>


Post some real sample input and expected output or we can't give you
anything better than a guess.

Ed.
apogeusistemas@gmail.com

2007-05-29, 7:21 pm

On 29 maio, 16:45, Ed Morton <mor...@lsupcaemnt.com> wrote:
> apogeusiste...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
)}' file[vbcol=seagreen]
>
>
>
>
>
>
>
>
>
>
>
> Post some real sample input and expected output or we can't give you
> anything better than a guess.
>
> Ed.- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -



I need find DAY128 string after "media id" string:
solaris> cat file

TIME SERVER/CLIENT TEXT
05/29/2007 15:18:35 smmtzsux001-bkp smmtzslx002 client smmtzslx002
handling
path /full_CORP_on_4fiiupcc
05/29/2007 15:18:35 smmtzsux001-bkp smmtzslx002 starting backup job
(jobid=3D207,827)
05/29/2007 15:18:43 smmtzsux001-bkp smmtzslx002 begin writing backup
id
smmtzslx002_1180441115, copy 1, fragment 1, to
media id
DAY128 on drive index 5

Ed Morton

2007-05-29, 7:21 pm

apogeusistemas@gmail.com wrote:

<snip>
> I need find DAY128 string after "media id" string:
> solaris> cat file
>
> TIME SERVER/CLIENT TEXT
> 05/29/2007 15:18:35 smmtzsux001-bkp smmtzslx002 client smmtzslx002
> handling
> path /full_CORP_on_4fiiupcc
> 05/29/2007 15:18:35 smmtzsux001-bkp smmtzslx002 starting backup job
> (jobid=207,827)
> 05/29/2007 15:18:43 smmtzsux001-bkp smmtzslx002 begin writing backup
> id
> smmtzslx002_1180441115, copy 1, fragment 1, to
> media id
> DAY128 on drive index 5
>


Is your input file really split such that "media id" occurs on it's own
on the line after "to" and before the line that starts with "DAY128"?
The solutions will be pretty different based on whether or not what
we're seeing is caused by line-wrapping of your posting.

Ed.
apogeusistemas@gmail.com

2007-05-29, 7:21 pm

On 29 maio, 17:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
> apogeusiste...@gmail.com wrote:
>
> <snip>
>
>
>
> Is your input file really split such that "media id" occurs on it's own
> on the line after "to" and before the line that starts with "DAY128"?
> The solutions will be pretty different based on whether or not what
> we're seeing is caused by line-wrapping of your posting.
>
> Ed.


to media id DAY128 on drive index 5

Ed Morton

2007-05-29, 7:21 pm

apogeusistemas@gmail.com wrote:
> On 29 maio, 17:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> to media id DAY128 on drive index 5
>


awk 'sub(/.*media id /,""){print $1}' file

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com