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.
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)
^
ensure we start at the beginning of a line(?<!\n)
is a negative lookbehind to make sure the current line is not preceeded by another one (i.e. we start counting from the first line)(?:...)
delimits a non capturing group. It's not the relevant part of the match.*?\n
matches any character until a line break is met (?
makes the expression lazy instead of greedy){2}
is the numer of line breaks you want to count (basically your line number minus 1).*?(line)
matches any character until the next occurrence of "line"See here: https://regex101.com/r/JIRkpN/1