How to match the Nth word of a line containing a specific word using regex

You can use this to match a line containing second and grab the 7th word:

^(?=.*\bsecond\b)(?:\S+ ){6}(\S+)

Make sure that the global and multiline flags are active.

^ matches the beginning of a line.

(?=.*\bsecond\b) is a positive lookahead to make sure there's the word second in that particular line.

(?:\S+ ){6} matches 6 words.

(\S+) will get the 7th.

regex101 demo


You can apply the same principle with other requirements.

With a line containing red and getting the 4th word...

^(?=.*\bred\b)(?:\S+ ){3}(\S+)

You asked for regex, and you got a very good answer.

Sometimes you need to ask for the solution, and not specify the tool.

Here is the one-liner that I think best suits your need:

awk '/second/ {print $7}' < inputFile.txt

Explanation:

/second/     - for any line that matches this regex (in this case, literal 'second')
print $7     - print the 7th field (by default, fields are separated by space)

I think it is much easier to understand than the regex - and it's more flexible for this kind of processing.

Tags:

Regex