简体   繁体   中英

find multi-line string in text file using powershell

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"
} 

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM