I have a large logfile and I want to extract (write to a new file) certain rows. The problem is I need a certain row and the row before. So the regex should be applied on more than one row. Notepad++ is not able to do that and I don’t want to write a script for that.
I assume I can do that with Powershell and a one-liner, but I don’t know where to start …
The regular expression is not the problem, will be something like that ^#\d+.*?\n.*?Failed.*?$
So, how can I open a file using the Powershell, passing the regex and get the rows back that fits my expression?
Look at
Select-Stringand-contextparameter:If you only need to display the matching line and the line before, use
(for a test I use my log file and my regex – the date there)
If you need to manipulate it further, store the result in a variable and use the properties:
If there are more lines that match the regex, access them with brackets: