0

My script finds a pattern and outputs all lines up to the pattern line into output.txt. But I get these added references in my output file when all I want is my original text.

  readfile.txt:2:
  readfile.txt:3:Original text in readfile.txt
  readfile.txt:4:next line of text from readfile.txt
  readfile.txt:5:blah blah
  readfile.txt:6:down to pattern
  readfile.txt:7:removed pattern line from the file below here

The relevant line in the script is:

# select the 6 lines before pattern 001 pattern
Select-String -Pattern '001' -Path readfile.txt -list -SimpleMatch -Context 6, 0 > output.txt

How do I get rid of the readfile.txt:2, readfile.txt:3, .. etc references?

i have searched the docs and googled. Any suggestion appreciated.

Dave
  • 687
  • 7
  • 15
  • 2
    Change `> output.txt` for `| Select-Object -Expand Line | Out-File output.txt` – Santiago Squarzon Jan 06 '22 at 00:03
  • Thank you. But this only gives me an empty output.txt file – Dave Jan 06 '22 at 00:20
  • That's odd, can you edit your question showing the complete command? – Santiago Squarzon Jan 06 '22 at 00:25
  • This is the complete command. The only Select_Object parameter would be ExpandProperty And IMHO this does not relate to file output https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2 – Dave Jan 06 '22 at 00:31
  • 2
    I meant the complete command you're running, from `Select-String` until the end. And for your information, `Select-String` returns an object of the type _MatchInfo_ which contains a property with the name `Line`. It relates to how you want to export your output. – Santiago Squarzon Jan 06 '22 at 00:34

0 Answers0