0

I am using powershell to filter a textfile using a regular expression. To do that I am using the following command: Select-String -Pattern "^[0-9]{2}[A-Z]{2}[a-z]{5}" -CaseSensitive rockyou.txt > filter.txt The issue however, when writing them to filter.txt it's preceding the matched strings with the name of the original file followed by the line numbers e.g.:

rockyou.txt:12345:abcdefg

rockyou.txt:12345:abcdefg

rockyou.txt:12345:abcdefg

How can I make it so that it ommits the line numbers?

ASlackjaw
  • 11
  • 1
  • 4
  • Pipe it. something like ```Select-String -Pattern "^[0-9]{2}[A-Z]{2}[a-z]{5}" -CaseSensitive rockyou.txt | $_.split(":")[-1] | Out-File(filter.txt)``` – Brian Feb 20 '20 at 19:19
  • Wouldn't a matching string be something like `01ABcdefg`? – js2010 Feb 20 '20 at 20:26

2 Answers2

2

Select-String outputs an object per match, and each has a Line property containing the original line in which the match occurred. You can grab only the Line value, like so:

... |Select-String ... |Select-Object -ExpandProperty Line |Out-File filter.txt
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

This way seems to work. Set-content saves the string version of the matchinfo object without any extra blank lines, as opposed to out-file or ">".

get-content rockyou.txt | select-string '^[0-9]{2}[A-Z]{2}[a-z]{5}' -ca | 
  set-content filter.txt
get-content filter.txt

01ABcdefg

It occurred to me you might still want the filename:

select-string '^[0-9]{2}[A-Z]{2}[a-z]{5}' rockyou.txt -ca | 
  % { $_.filename + ':' + $_.line } > filter.txt
cat filter.txt

rockyou.txt:01ABcdefg

js2010
  • 23,033
  • 6
  • 64
  • 66