Parsing line after match “something” Powershell

Question:

I’m trying to parse a .txt file in order to create a .csv, the issue is that the file is too big and I don’t have similar characters in all the txt, so this is a short example:

This txt file has got 500 lines. I think that if I could match one word like “hostname” i would be able to select the next line.

Something like:

But I don’t know how to select the line after Hostname.

Answer:

You could use Select-String and the -Context parameter to do this:

The -Context parameter can be used to select a number of lines before/after the line that matched. If you provide a single digit input you get that number of lines before and after. By specifying two numbers you specify how many lines before and how many after (so in the example above 0 before and 1 after).

The result is returned in to a property named .Context which has a .PostContext sub-property containing the result of the line/s after the match.

Source:

Parsing line after match “something” Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply