How to copy a line that is 3 lines before a particular string

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
vs.vivek1 [ Do, 17 Januar 2008 15:15 ] [ ID #1910201 ]

Re: How to copy a line that is 3 lines before a particular string

At 2008-01-17 09:15AM, "vs.vivek1 [at] 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
Glenn Jackman [ Do, 17 Januar 2008 15:40 ] [ ID #1910203 ]

Re: How to copy a line that is 3 lines before a particular string

On 17 Jan., 15:40, Glenn Jackman <gle... [at] ncf.ca> wrote:
> At 2008-01-17 09:15AM, "vs.viv... [at] 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=3D=3D"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=3D=3D"failed"{print $1}'


Janis
Janis Papanagnou [ Do, 17 Januar 2008 15:57 ] [ ID #1910205 ]

Re: How to copy a line that is 3 lines before a particular string

vs.vivek1 [at] 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)
PK [ Do, 17 Januar 2008 16:14 ] [ ID #1910206 ]

Re: How to copy a line that is 3 lines before a particular string

On Jan 17, 8:14=A0pm, pk <p... [at] pk.pk> wrote:
> vs.viv... [at] 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
Anup V [ Do, 17 Januar 2008 16:34 ] [ ID #1910207 ]

Re: How to copy a line that is 3 lines before a particular string

On 2008-01-17, varkey <anupvarghese [at] gmail.com> wrote:
>
>
> grep -B is not working in some solaris versions
>
You need GNU grep, or use awk (nawk or usr/xpg4/bin/awk)
Bill Marcum [ Do, 17 Januar 2008 17:22 ] [ ID #1910208 ]

Re: How to copy a line that is 3 lines before a particular string

On 1/17/2008 8:57 AM, Janis wrote:
> On 17 Jan., 15:40, Glenn Jackman <gle... [at] ncf.ca> wrote:
>
>>At 2008-01-17 09:15AM, "vs.viv... [at] 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.
Ed Morton [ Do, 17 Januar 2008 22:06 ] [ ID #1910220 ]
Linux » comp.unix.shell » How to copy a line that is 3 lines before a particular string

Vorheriges Thema: election ballot editer in (Ba)sh demo'ed on YouTube
Nächstes Thema: How To Tell If Your Dog Doesnt Understand Your Command