1

I'm trying to find the multi line string in the text file using below script but it's not working as expected.

$tns= "MYSID=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = mydnshostname)(PORT = 1111))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = MYSID)
    )
  )"

$SEL = Select-String -Path T:\Test\search.txt -Pattern $tns


if ($SEL -ne $null)
{
    Write-Host "Contains String"
}
else
{
    Write-Host "Not Contains String"
} 
4
  • The code is not working* Commented Oct 3, 2020 at 13:44
  • 1
    when you use Select-String, the -Pattern is a regex pattern. your pattern contains multiple special regex chars [() for instance]. try using -SimpleMatch to get a non-regex pattern match. Commented Oct 3, 2020 at 14:03
  • or do Select-String ... -Pattern $([regex]::Escape($tns)) Commented Oct 3, 2020 at 14:04
  • please post a realistic sample of the input file you are working with, AND the exact info you want from it. i doubt that you really want that whole multiline string. i suspect you can use a simpler pattern to get the actual info you want. Commented Oct 3, 2020 at 15:01

1 Answer 1

1

It depends on the file's line endings. In windows, lines ending in \r\n, one way is this. With a match, it will return the whole file.

echo one '(two)' three four five | set-content file.txt
get-content -raw file.txt | select-string '\(two\)\r\nthree\r\nfour'

one
(two)
three
four
five

Or using regex single line mode (?s) where a . can match a line ending. It should work with unix line endings too \n . Without the pipe, select-string can't seem to match multiple lines (-path parameter).

get-content -raw file.txt | select-string '(?s)\(two\).*three.*four'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.