regex

RegEx: Search string at specified line of (text-)file


Is there a RegEx-approach to search in a given (text-)file only at a specified line? I'm using the tool "grepWin".

If my text-file was for example:

This is the first line apple
This is the second line orange
This is the third line apple
This is the fourth line orange

How can I search and find only the string "apple" in the third line?

I also want to search files for strings that occur at specific lines in the file. Like I am looking for 'Error Foo', but only if it happens in line 4 of the error log.


Solution

  • There is a regex approach but you might want to split your text with split('\n') (or similar depending on the language).
    Anyway you'll have what you're looking for with:

    ^(?<!\n)(?:.*?\n){2}[^\n]*?(line)
    

    See here: https://regex101.com/r/JIRkpN/1