as shelter commented, at beginning the A
wasn't assigned by a value, awk will take 0
, so 0+1, 0+2
... you see why you got the unexpected line.
you don't have to play with NR
for your needs, if I understood your requirement right.
try this line:
awk '$1=="3"{getline;print;exit}' file
test with your example:
kent$ echo "1
A
2
B
3
C"|awk '$1=="3"{getline;print;exit}'
C
If you just want to fix the problem in your code, you add a &&A
, like:
awk '{if ($1=="3") A=NR}{ if (NR==(A+1)&&A) print $0}'
and your line could be shorten as:
awk '$1=="3"{A=NR}NR==(A+1)&&A'