locked
select-string / multiple matches RRS feed

  • Question

  • This may be easy, and I suspect it has to do with the selection being inside an "if" statement:

                    if (Select-String -Path "$filepresent" -Pattern "ABNORMAL" -Quiet)
                        # WHERE "ABNORMAL" IS PRESENT IN THE FILE
                        {
                        Write-Host "ABNORMAL RESTART PRESENT in $filepresent." -ForegroundColor Yellow -BackgroundColor DarkGreen
                        $abbystring = (Select-String -Path "$filepresent" -Pattern "ABNORMAL")
                        Write-Host "Line number: "$abbystring.LineNumber", Text: "$abbystring.Line
                        }
                    else 
                        {
                        # Write-Host "String not present in $filepresent." -ForegroundColor Yellow -BackgroundColor DarkRed
                        }

    Output is similar to this:

    ABNORMAL RESTART PRESENT in \\20067SURE17\d$\Program Files\POSWIN\upload\keep51\0000671732.jrn.
    Line number:  61 3073 , Text:              ABNORMAL SHUTDOWN             ABNORMAL SHUTDOWN

    You can see multiple occurrences of the string "ABNORMAL" are present in the files searched.  The question I have is - when there are multiple matches within the same file, is there a means by which to list each line and the text separately :

    ABNORMAL RESTART PRESENT in \\20067SURE17\d$\Program Files\POSWIN\upload\keep51\0000671732.jrn.
    Line number:  61  , Text:              ABNORMAL SHUTDOWN             
    Line number:  3073 , Text:              ABNORMAL SHUTDOWN             

    In this case, I'm asking re: Aesthetics, but it could be relevant if I use similar code to search the files.

    Input is greatly appreciated.

    Regards,

    Ben





    Friday, May 20, 2016 8:03 PM

Answers

  • Use Get-ChildItem to select the needed files, Select-String to find the needed string, and output the Path, LineNumber, and Line properties from each match.

    Get-ChildItem "d:\path\*.jrn" | Select-String 'abnormal' | ForEach-Object {
      $_ | Select-Object Path,LineNumber,Line
    }
    

    This outputs objects, so you can pipe to Sort-Object to sort, Export-Csv to create a CSV file, etc.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Thursday, June 23, 2016 10:44 PM
    • Marked as answer by BAWrites Friday, June 24, 2016 12:34 AM
    Thursday, June 23, 2016 10:36 PM

All replies

  • I do it differently.  I'm sure there are faster ways, but it runs fast enough for my needs.

    $str = Read-Host "Enter string to fine"

    $dir = Read-Host "Enter directory to search"

    $files = Get-ChildItem -Path $dir -File -Recurse foreach ($file in $files) { $lines = Get-Content -Path $file.FullName $i = 0 $newFile = $true foreach ($line in $lines) { $i++ If ($line -match $str) { If ($newFile) { Write-Host $file.FullName -ForegroundColor Cyan $newFile = $false } Write-Host "Line:$i $line" -ForegroundColor yellow } } }



    . : | : . : | : . tim

    Friday, May 20, 2016 9:41 PM
  • You want to loop over all items if there are more than one in $abbystring. Something like this

    $abbystring | ForEach-Object { Write-Host "Line number: $($_.LineNumber) Text: $($_.Line)" }



    Friday, May 20, 2016 11:01 PM
  • Use Get-ChildItem to select the needed files, Select-String to find the needed string, and output the Path, LineNumber, and Line properties from each match.

    Get-ChildItem "d:\path\*.jrn" | Select-String 'abnormal' | ForEach-Object {
      $_ | Select-Object Path,LineNumber,Line
    }
    

    This outputs objects, so you can pipe to Sort-Object to sort, Export-Csv to create a CSV file, etc.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Thursday, June 23, 2016 10:44 PM
    • Marked as answer by BAWrites Friday, June 24, 2016 12:34 AM
    Thursday, June 23, 2016 10:36 PM
  • I'm going to check these all out @ work - the last answer looks the least complex and I get it - but each has something I didn't know!  Thanks all.
    Friday, June 24, 2016 12:34 AM