regex - AWK display the line number of last match -


i'm new awk. know how print out line number of last match of file using awk. here's small part of test.txt file content:

close #140,value=140 wait = #14039,value=143 close #140,value=144 wait #0,value=155 wait = #14039,value=158 close #140,value=160 

this code used far success first line:

awk -f= '{if($nf >= 143 && $nf <= 158){print nr,exit}}' test.txt 

but last line

awk -f= '{if($nf >= 143 && $nf <= 158){a=$0}} end{print a,nr}' test.txt 

it's printed out hold matching line , last line number of file. how can line number of last match? please me advice.

use a = nr instead of a = $0 (because it's line number want remember, not line itself).

apart that, arguably more awkish write

awk -f= '$nf >= 143 && $nf <= 158 { = nr } end { print }' test.txt 

{if(){}} bit ugly.


Comments