Jairaj
March 30, 2010, 4:23am
1
I want to print the current and next line of "Mar 02" in the file.
Input
Mon Mar 02 11:07:02 2009
ABC
Tue Mar 03 11:02:20 2010
Wrong data
Mon Mar 02 11:07:02 2009
XYZ
Mon Mar 02 11:07:08 2010
124
Mon Mar 02 11:07:08 2010
1400
Output
Mon Mar 02 11:07:02 2009
ABC
Mon Mar 02 11:07:02 2009
XYZ
Mon Mar 02 11:07:08 2010
124
Mon Mar 02 11:07:08 2010
1400
I am using below ;
awk '{if ($2 == "Mar" && $3 == "02") print $0}' ifile
but i am getting
Mon Mar 02 11:07:02 2009
Mon Mar 02 11:07:02 2009
Mon Mar 02 11:07:08 2010
Mon Mar 02 11:07:08 2010
I want to print next line also.. Your help is appreciated.
skmdu
March 30, 2010, 4:38am
2
sed -n '/Mar 02/{N;p}' infpufile
Jairaj
March 30, 2010, 4:43am
3
Perfect.
but ; after p is missing.
Correct one is :
sed -n '/Mar 02/{N;p;}' infpufile
Thanks a lot
clx
March 30, 2010, 5:14am
4
or,
awk '/Mar 02/{R=NR}NR==R || NR==R+1' file
or, if you have GNU grep,
grep -A1 'Mar 02' file
Hi anchal,
can you please explain the awk option in detail
clx
March 30, 2010, 6:30am
6
NR is a system variable in awk which gives the current record numner (line number) which is being read.
whenever awk finds 'Mar 02', it stores the line number in a variable R and in the the next step, it print the lines with line number R and R+1 (current and next).
awk '/Mar 02/{R=NR}NR<=(R+1)' file