none
Select-String over multiple lines RRS feed

  • Question

  • Hello,

    I am trying to run an IF statement against a string match from a file. from the file. i am looking to find a match on this:

    State           : Failed
    Severity        : Error

    I firstly load a variable like this

    $stringmatch = Get-Content "c:\test.txt"

    my if statement looks like this but gives me an error

    if(Select-String "State           : Failed`nSeverity        : Error" $stringmatch)

    {

    }

    I thought if i used the `n this would designate a new line in my string, the error i get when i run it is

    Select-String : Cannot bind argument to parameter 'Path' because it is an empty string.

    + if(Select-String "State           : Failed`nSeverity        : Error" $stringmatch)
    +                                                                      ~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Select-String], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand

    the $stringmatch variable does contain strings I have checked.

    thanks

    Steve

    Wednesday, August 13, 2014 12:02 PM

Answers

  • You've got a couple of problems there.

    Get-Content is going to return an array of strings, and Select-String is going to look at each one individually.  If you want to do a multi-line match, you have to give it a multi-line string to match to.  You can do this by adding the -Raw switch to Get-Content, or by piping the Get-Content output through Out-String if you're running an older version of PS.

    Next, you're trying to use -InputObject as a positional parameter in Select-String.  It is not positional (check the help on Select-String).  Only file path and pattern are positional.

    Finally, you're trying to use `n to match a newline in your regex.  That's a newline to the PS parser, but regex has it's own parser and it's own parsing rules.  To match a newline in the regex, you use \r\n.

    $stringmatch = Get-Content  -Raw -Path 'c:\test.txt'
    if(Select-String  -InputObject $stringmatch -pattern 'State           : Failed\r\nSeverity        : Error')
     {


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Milkientia Wednesday, August 13, 2014 2:05 PM
    Wednesday, August 13, 2014 12:43 PM
    Moderator

All replies

  • Try this code:

    $stringmatch = Get-Content .\test.txt
    for ($i=0; $i -lt $stringmatch.Length; $i++ ) {
        if ($stringmatch[$i].Contains("State") -and $stringmatch[$i].Contains("Failed")) {
            "State line:    $($stringmatch[$i])"
    
        }
        if ($stringmatch[$i].Contains("Severity") -and $stringmatch[$i].Contains("Error")) {
            "Severity line: $($stringmatch[$i])"
        }
    }


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable)

    Wednesday, August 13, 2014 12:29 PM
  • You've got a couple of problems there.

    Get-Content is going to return an array of strings, and Select-String is going to look at each one individually.  If you want to do a multi-line match, you have to give it a multi-line string to match to.  You can do this by adding the -Raw switch to Get-Content, or by piping the Get-Content output through Out-String if you're running an older version of PS.

    Next, you're trying to use -InputObject as a positional parameter in Select-String.  It is not positional (check the help on Select-String).  Only file path and pattern are positional.

    Finally, you're trying to use `n to match a newline in your regex.  That's a newline to the PS parser, but regex has it's own parser and it's own parsing rules.  To match a newline in the regex, you use \r\n.

    $stringmatch = Get-Content  -Raw -Path 'c:\test.txt'
    if(Select-String  -InputObject $stringmatch -pattern 'State           : Failed\r\nSeverity        : Error')
     {


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Milkientia Wednesday, August 13, 2014 2:05 PM
    Wednesday, August 13, 2014 12:43 PM
    Moderator
  • This might be needed:

    $stringmatch = Get-Content  -Raw -Path 'c:\test.txt'
    $pattern= 'State\s*:\s*Failed\s*\r\nSeverity\s*:\s*Error'
    if(Select-String  -InputObject $stringmatch -pattern $pattern)

    \s should eliminate counting spaces or tabs or any other blank stuff.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, August 13, 2014 1:08 PM
    Wednesday, August 13, 2014 12:50 PM
  • Might want to add a quantifier to those \s metacharacters. \s by itself is only going to match one.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Wednesday, August 13, 2014 1:00 PM
    Moderator
  • Might want to add a quantifier to those \s metacharacters. \s by itself is only going to match one.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Right - thanks.

    ¯\_(ツ)_/¯

    Wednesday, August 13, 2014 1:07 PM
  • thank you, this worked perfectly. cheers for that.

    Steve

    Wednesday, August 13, 2014 2:05 PM