Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove filename from Select-String results with Context option in Powershell?

Tags:

powershell

Without -Context option I could easily choose just select line without filename from Select-String results. But this is not a case when I use Select-String -Context x,y option.

Is there a way to hide filename from Context lines?

like image 382
Dima Fomin Avatar asked Oct 20 '25 14:10

Dima Fomin


1 Answers

No, you can also run Select-String -context x,y ... | select line, providing -context parameter to Select-String, but you'll just omit the relevant context altogether in your output.

You seem to desire to override the default behavior of MatchInfo::ToString() writing filename with lines. You can do like this:

Select-String -Context x,y .... |
  % {$_.context.precontext;$_.line;$_.context.postcontext}

This will output a set of strings with an unmarked matching line, but this should give you a start on formatting output of Select-String.

like image 117
Vesper Avatar answered Oct 23 '25 03:10

Vesper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!