Search string with Get-Content and show x lines before/after result

Microsoft Windows
Post Reply
UnRegistrado
Posts: 2
Joined: 2021-Jul-27, 7:57 am

Search string with Get-Content and show x lines before/after result

Post by UnRegistrado »

I need to search a string in a file, and it show me the string and their x lineas before/after string, I tryed several sintaxis but I don´t know how do it. Could someone help me?

Example:
Get-Content -Path C:\Mi-log.txt | where { $_ -match "text-I-want-to-search" } -tail 5
JoeC4281
Posts: 8
Joined: 2021-Jul-26, 12:51 pm

Re: Search string with Get-Content and show x lines before/after result

Post by JoeC4281 »

Select-String may solve your problem.

An example;

Code: Select all

powershell -command "$f = type c:\utils\astronomy.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11; $f"
Ref: https://ss64.com/ps/select-string.html
Ref: https://www.jpsoft.com/forums/threads/s ... text.8773/

Joe
Post Reply