How to copy a line that is 3 lines before a particular string
How to copy a line that is 3 lines before a particular string
am 17.01.2008 15:15:17 von vs.vivek1
if i have a file
like
cmd1;
cmd2
cmd3
failed
cmd3
cmd
cmd
success
cmd1
cmd3
cmk
failed
My requirement is to grep all lines that is 3 lines behind the string
failed. ie cmd1 in above said case
please help
Thanks and Regards;
Vivek
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 15:40:39 von Glenn Jackman
At 2008-01-17 09:15AM, "vs.vivek1@gmail.com" wrote:
> if i have a file
> like
>
> cmd1;
> cmd2
> cmd3
> failed
>
> cmd3
> cmd
> cmd
> success
>
> cmd1
> cmd3
> cmk
> failed
>
>
> My requirement is to grep all lines that is 3 lines behind the string
> failed. ie cmd1 in above said case
This approach simply remembers the previous 3 lines, and prints the 3rd
line when it sees "failed":
awk '/failed/ {print L3} {L3=L2; L2=L1; L1=$0}' afile
It'll print a blank line if "failed" occurs in the first 3 lines of the
file.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 15:57:05 von Janis Papanagnou
On 17 Jan., 15:40, Glenn Jackman wrote:
> At 2008-01-17 09:15AM, "vs.viv...@gmail.com" wrote:
>
>
>
>
>
> > =A0if i have a file
> > =A0like
>
> > =A0cmd1;
> > =A0cmd2
> > =A0cmd3
> > =A0failed
>
> > =A0cmd3
> > =A0cmd
> > =A0cmd
> > =A0success
>
> > =A0cmd1
> > =A0cmd3
> > =A0cmk
> > =A0failed
>
> > =A0My requirement is to grep all lines that is 3 lines behind the string=
> > =A0failed. =A0ie cmd1 in above said case
>
> This approach simply remembers the previous 3 lines, and prints the 3rd
> line when it sees "failed":
>
> =A0 =A0 awk '/failed/ {print L3} {L3=3DL2; L2=3DL1; L1=3D$0}' afile
>
> It'll print a blank line if "failed" occurs in the first 3 lines of the
> file.
>
> --
> Glenn Jackman
> "You can only be young once. But you can always be immature." -- Dave Barr=
y
Here's a variant (with less copying) in case we can assume that
"failed" is the last line of every block...
awk -v RS=3D"" '$NF=="failed"{print $(NF-3)}'
or (more terse) if the interesting command is always in the first line
of a block...
awk -v RS=3D"" '$NF=="failed"{print $1}'
Janis
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 16:14:16 von PK
vs.vivek1@gmail.com wrote:
> if i have a file
> like
>
> cmd1;
> cmd2
> cmd3
> failed
>
> cmd3
> cmd
> cmd
> success
>
> cmd1
> cmd3
> cmk
> failed
>
>
> My requirement is to grep all lines that is 3 lines behind the string
> failed. ie cmd1 in above said case
Really dumb solution:
grep -B 3 failed file.txt | sed -n '1~5p' (GNU sed only)
grep -B 3 failed file.txt | sed -n '1,${p;n;n;n;n;}' (all versions)
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 16:34:17 von Anup V
On Jan 17, 8:14=A0pm, pk
wrote:
> vs.viv...@gmail.com wrote:
> > if i have a file
> > like
>
> > cmd1;
> > cmd2
> > cmd3
> > failed
>
> > cmd3
> > cmd
> > cmd
> > success
>
> > cmd1
> > cmd3
> > cmk
> > failed
>
> > My requirement is to grep all lines that is 3 lines behind the string
> > failed. =A0ie cmd1 in above said case
>
> Really dumb solution:
>
> grep -B 3 failed file.txt | sed -n '1~5p' =A0 (GNU sed only)
>
> grep -B 3 failed file.txt | sed -n '1,${p;n;n;n;n;}' =A0 =A0 (all versions=
)
grep -B is not working in some solaris versions
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 17:22:20 von Bill Marcum
On 2008-01-17, varkey wrote:
>
>
> grep -B is not working in some solaris versions
>
You need GNU grep, or use awk (nawk or usr/xpg4/bin/awk)
Re: How to copy a line that is 3 lines before a particular string
am 17.01.2008 22:06:07 von Ed Morton
On 1/17/2008 8:57 AM, Janis wrote:
> On 17 Jan., 15:40, Glenn Jackman wrote:
>
>>At 2008-01-17 09:15AM, "vs.viv...@gmail.com" wrote:
>>
>>
>>
>>
>>
>>
>>> if i have a file
>>> like
>>
>>> cmd1;
>>> cmd2
>>> cmd3
>>> failed
>>
>>> cmd3
>>> cmd
>>> cmd
>>> success
>>
>>> cmd1
>>> cmd3
>>> cmk
>>> failed
>>
>>> My requirement is to grep all lines that is 3 lines behind the string
>>> failed. ie cmd1 in above said case
>>
>>This approach simply remembers the previous 3 lines, and prints the 3rd
>>line when it sees "failed":
>>
>> awk '/failed/ {print L3} {L3=L2; L2=L1; L1=$0}' afile
>>
>>It'll print a blank line if "failed" occurs in the first 3 lines of the
>>file.
>>
>>--
>>Glenn Jackman
>>"You can only be young once. But you can always be immature." -- Dave Barry
>
>
> Here's a variant (with less copying) in case we can assume that
> "failed" is the last line of every block...
>
> awk -v RS="" '$NF=="failed"{print $(NF-3)}'
>
> or (more terse) if the interesting command is always in the first line
> of a block...
>
> awk -v RS="" '$NF=="failed"{print $1}'
You MIGHT also need a -F'\n' if the lines can contain white space.
Ed.