Page 1 of 1

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

Posted: 2022-Dec-26, 12:23 pm
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

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

Posted: 2023-Mar-27, 12:01 pm
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