17

Using the following Select-String command in PowerShell:

Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches > E:\Documents\combined3.txt

creates an output file with each line starting with the path and file name followed by a colon. For example:

E:\Documents\combined0.txt:255:255.255.255 - - [31/Dec/2014:04:15:16 -0800] "GET /ccsetup.exe HTTP/1.1" 301 451 "-" "Mozilla/5.0 (compatible; SearchmetricsBot; http://www.xxxx.com/en/xxxx-bot/)"

How do I get rid of the output file path name, output file name and colon in the results?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rob
  • 3,488
  • 3
  • 32
  • 27

7 Answers7

31

Select-String outputs an object from which you can pick off properties that you want. The Get-Member command will show you these object members if you pipe into it e.g.:

Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches  | 
    Get-Member

One of those properties is Line. So try it this way:

Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches | 
    Foreach {$_.Line} > E:\Documents\combined3.txt
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
13

As usual powershell returns things as objects, by default select-string returns several properties including LineNumber, Filename, etc; the one you want with the data in is just called "Line". So no need for anything fancy, just pipe it to "select line".

Eg:

Select-String "bla" filename.txt | select-object -ExpandProperty Line | out-file E:\bla_text.txt
Community
  • 1
  • 1
AutoMattTick
  • 453
  • 4
  • 6
1

If you're looking for (sub)strings rather than patterns, using the -like operator might be a better approach, performance-wise and with respect to ease-of-use.

$searchString = 'GET /ccsetup.exe'

Get-Content 'E:\Documents\combined0.txt' |
  ? { $_ -like "*$searchString*" } |
  Set-Content 'E:\Documents\combined3.txt'

If you do need pattern matches, you can easily replace the -like operator with the -match operator:

$pattern = 'GET /ccsetup\.exe'

Get-Content 'E:\Documents\combined0.txt' |
  ? { $_ -match $pattern } |
  Set-Content 'E:\Documents\combined3.txt'
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Elegant solution:

PS> type .\test.txt
abc 
abc123 
xyz 
xyz123

PS> $(Select-String "123" .\test.txt).Line
abc123 
xyz123
Fernando Fabreti
  • 4,277
  • 3
  • 32
  • 33
0
Get-Content E:\Documents\combined0.txt | Select-String -Pattern "GET /ccsetup\.exe" -AllMatches
Frits
  • 7,341
  • 10
  • 42
  • 60
Fredrick
  • 1,210
  • 2
  • 15
  • 24
0
# define your search path
$files = Get-ChildItem "./some_path"


for ($i=0; $i -lt $files.Count; $i++) {
    # loop through files in search folder
    $x=Select-String -Path $files[$i].FullName -Pattern "whatYouSearch"
    # retrieve the info with the option Line
    $out=$x.Line 
    # echo to output file (append)
    $out >> result.csv
}
  • Hey Paul, it is a different approach to the same question. Plus you have the loop variable $i in the control flow, so that you can implement conditions to pipe/append/initialize. – Domenico Spidy Tamburro Apr 25 '19 at 09:07
0

I found this and wanted a simpler method of filtering out the exact text. I used "| select-object -ExpandProperty Line" at the end of "Select-String -Path C:\Temp\AccountLockoutPolicy.Txt -Pattern 'ResetLockoutCount ='" and it removed the path and the line number. I like simple!

  • Welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Oct 08 '20 at 16:43