[EMAIL PROTECTED] wrote:
[fixed below]
----- Original Message -----
From: Dimitar Radoulov <[EMAIL PROTECTED]>
On Feb 17, 2008 1:13 PM, Radoulov, Dimitre <[EMAIL PROTECTED]>
wrote:> [EMAIL PROTECTED] wrote:
[...]
I want to print a line which contain a string with 4 lines
above 6 lines below to an output file. Here is the example:
[...]
awk '{ x[FNR] = $0 }
f && f--
/dock/ { for (i=FNR-4; i<=FNR; i++)
print x[i]
split("",x)
f = 6 }' filename
Just to add that you should use nawk or /usr/xpg4/bin/awk
with this code.
Another version
(works with /usr/xpg4/bin/awk and GNU Awk):
awk '{ x[FNR] = $0 }
/doc/ {
min = FNR - 4
max = FNR + 6
}
FNR == max {
for (i=min; i<=max; i++)
print x[i]
delete x
}' filename
[...]
> How can I re-write the same to print only the lines
> which has the matching string, and just the 4th line before that
> and sixth line after that?
/usr/xpg4/bin/awk '{ x[FNR] = $0 }
$0 ~ p { r = FNR}
r && FNR == r + 6 {
printf "%s\n%s\n%s\n",
x[r-4], x[r], x[r+6]
delete x
}' p="^dock" filename
Or:
nawk '{ x[FNR] = $0 }
$0 ~ p { r = FNR}
r && FNR == r + 6 {
printf "%s\n%s\n%s\n",
x[r-4], x[r], x[r+6]
split("",x)
}' p="^dock" filename
Regards
Dimitre
_______________________________________________
Solaris-Users mailing list
[email protected]
http://www.filibeto.org/mailman/listinfo/solaris-users