Text Processing – Print Line After nth Occurrence of a Match

awkregular expressionsedtext processing

I am looking to display the line 4598 in the following file. Effectively I want to display the line AFTER the nth occurrence of a match. In this case, the line after the 3rd occurrence of <Car>. How do I go about this?

<Car>
10456
</Car>
<Car>
70192
</Car>
<Car>
4598
</Car>

Best Answer

awk -v n=3 '/<Car>/ && !--n {getline; print; exit}'

Or:

awk '/<Car>/ && ++n == 3 {getline; print; exit}'

To pass the search pattern as a variable:

var='<car>'
PATTERN="$var" awk -v n=3 '
  $0 ~ ENVIRON["PATTERN"] && ++n == 3 {getline; print; exit}'

Here using ENVIRON instead of -v as -v expands backslash-escape sequences and backslashes are often found in regular expressions (so would need to be doubled with -v).

GNU awk 4.2 or above lets you assign variables as strong typed regexps. As long as its POSIX mode is not enabled (for instance via the $POSIXLY_CORRECT environment variable, you can do:

# GNU awk 4.2 or above only, when not in POSIX mode
gawk -v n=3 -v pattern="@/$var/" '
  $0 ~ pattern && ++n == 3 {getline; print; exit}'
Related Question