none
Searching for text in file spanning multiple lines RRS feed

  • Question

  • hi. i would like to search for a specific combination of strings in a file to essentially make sure that a test was performed successfully. my source file contains a line followed by a carriage return (`r) and then a new line (`n) which then contains the rest of the string i need to match.

    how should i format my select-string statement?

    i currently have:

    Select-String -Pattern "some text `r`nrest of my text." -Quiet

    Monday, September 21, 2015 8:23 AM

Answers

  • f(Get-Content test.txt|out-string |select-string -Pattern "(?m)THis is a test\r\nNot a test" -Quiet){
         'Match found'
    }else{
         'No match found'
    }


    \_(ツ)_/

    Monday, September 21, 2015 12:55 PM

All replies

  • You could try the following:

    $path = "<yourFilePath>"
    $pattern = '(.*some text\r\nrest of my text.*)'
    $input = ( Get-Content $path | Out-String ).Trim()
    foreach ($i in [Regex]::Matches($input, $pattern,  [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)) 
        {$i.Value}
    

    , where the dot (.) matches any single character except "\n".

    wizend

    Monday, September 21, 2015 9:44 AM
  • thanks. how would i get this to return a boolean as to whether or not the for loop was able to match each character?
    Monday, September 21, 2015 9:54 AM
  • You could use the "Success" property of the Match class instead of "Value", so replace $i.Value with $i.Success.

    wizend

    Monday, September 21, 2015 10:18 AM
  • ok. your method is probably more effeciant, but i just want to search for this entire phrase and receive false if not found or true if found. my decision structure evaluates this as follows:

    ElseIf (-not(Get-Content -Path C:\scripts\test.txt | Select-String -Pattern 'Validating site costed referrals in DC5.\r\nSite costing is enabled on SYSVOL/NETLOGON referrals.' -Quiet))

    can you tell me why the \r\n do not work in this instance?

    Monday, September 21, 2015 12:10 PM
  • Search for text in a file:

    if(Select-String -Path myfile.txt -pattern 'some text to find'){
         'text found'
    }else{
        'text not found'
    }


    \_(ツ)_/

    Monday, September 21, 2015 12:26 PM
  • i can do that on the other strings i have to find in the file, but because of the multiple carriage return characters its not matching for some reason. i also opened the file in word to check if there are indeed \r characters in there and it seems there are two of them. in word i had to open it as unicode which i then specified in the select-string command-let, but still no luck.

    currently the code listed below id evaluating to true.

    ElseIf (-not(Get-Content -Path C:\scripts\test.txt | Select-String -Pattern 'Validating site costed referrals in DC5.\r\rSite costing is enabled on SYSVOL/NETLOGON referrals.' -Quiet))

    Monday, September 21, 2015 12:40 PM
  • When using \r you must use double quotes or the meta characters will not be expanded.

    You also need to specify a multiline match.


    \_(ツ)_/

    Monday, September 21, 2015 12:42 PM
  • Here is how to get Select-String to do a multiline match;

    Get-Content test.txt|
        out-string |
        select-string -Pattern "(?m)THis is a test\r\nNot a test"


    \_(ツ)_/

    Monday, September 21, 2015 12:54 PM
  • f(Get-Content test.txt|out-string |select-string -Pattern "(?m)THis is a test\r\nNot a test" -Quiet){
         'Match found'
    }else{
         'No match found'
    }


    \_(ツ)_/

    Monday, September 21, 2015 12:55 PM
  • ok for some reason when i add the out-string component it matches the first line including the carriage return and newline special characters. however, when i add the text on the second line right after that it does'nt work. this works:

    If (-not(Get-Content -Path C:\scripts\test.txt | Out-String | Select-String -Pattern "(?m)Validating site costed referrals in DC5.\r\n" -Quiet))

    this doesn't and all i did was add the rest of the next line's text:

    If (-not(Get-Content -Path C:\scripts\test.txt | Out-String | Select-String -Pattern "(?m)Validating site costed referrals in DC5.\r\nSuccess: Site costing is enabled on SYSVOL/NETLOGON referrals." -Quiet))

    Monday, September 21, 2015 1:03 PM
  • finally i got it. i needed multiple carriage return and new line special characters even though i could only see a subset of them.

    thanks jrv.

    Monday, September 21, 2015 1:10 PM