Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在文件中搜索三行连续的文本_Windows_Powershell - Fatal编程技术网

Windows 在文件中搜索三行连续的文本

Windows 在文件中搜索三行连续的文本,windows,powershell,Windows,Powershell,问题:我需要在文件中搜索一些文本,它包含三行连续的文本。如何验证(查找)文件中是否存在这些行 预期返回值:布尔值 示例输入文件:text.txt 要搜索的示例模式 two three four 简单回答: $file = (Get-Content -Raw file.txt) -replace "`r" # removing "`r" if present $pattern = 'two three four' -replace "`r" $file | Select-String $p

问题:我需要在文件中搜索一些文本,它包含三行连续的文本。如何验证(查找)文件中是否存在这些行

预期返回值:布尔值


示例输入文件:text.txt

要搜索的示例模式

two
three
four

简单回答

$file = (Get-Content -Raw file.txt) -replace "`r" # removing "`r" if present

$pattern = 'two
three
four' -replace "`r"

$file | Select-String $pattern -Quiet -SimpleMatch
重新编辑。哇!这是一个棘手的方法。在提示符处,$pattern没有“`r”,但在脚本中有。这应该作为脚本或在提示下工作

$file = (get-content -raw file.txt) -replace "`r"

$pattern = 'two
three
four' -replace "`r"

# just showing what they really are
$file -replace "`r",'\r' -replace "`n",'\n'
$pattern -replace "`r",'\r' -replace "`n",'\n'

# 4 ways to do it
$file -match $pattern
$file | select-string $pattern -quiet -simplematch
$file -like "*$pattern*"
$file.contains($pattern)

# output
one\ntwo\nthree\nfour\nfive\n
two\nthree\nfour
True
True
True
True
嗯,试着用正则表达式。在单线模式下,a。可以匹配一个“`r”或一个“`n”


你应该看看关于提问的技巧,这样你就可以得到高质量、量身定制的回答。响应者希望被跟踪。对于一些指针,考虑和。三行连续写入文件中吗?行之间是否有换行和回车?如果在文件中找到行,您希望代码输出什么?如果找不到行,您希望代码输出什么?您需要知道匹配文本的行号吗?区分大小写重要吗?文本文件有多大?这段代码需要遍历许多文本文件吗?我们可以提供一般性的答案,但它们可能不是您所需要的。代码示例会很有帮助。@AdminOfThings是的,它们与新行/回车符是连续的,我只需要知道这些行的存在。在Select字符串上使用-SimpleMatch参数,或者在Get内容变量上使用-match来查找模式,如果($found){do SOUTH}当我运行它们(全部6个测试用例)时,它们的计算结果都为false。您是如何生成$file和$pattern的?它必须和我做的一样。我只是把你的脚本复制到一个ps1文件中并运行它。这很有效,谢谢。转义的换行符和回车符是有意义的。
$file = (get-content -raw file.txt) -replace "`r"

$pattern = 'two
three
four' -replace "`r"

# just showing what they really are
$file -replace "`r",'\r' -replace "`n",'\n'
$pattern -replace "`r",'\r' -replace "`n",'\n'

# 4 ways to do it
$file -match $pattern
$file | select-string $pattern -quiet -simplematch
$file -like "*$pattern*"
$file.contains($pattern)

# output
one\ntwo\nthree\nfour\nfive\n
two\nthree\nfour
True
True
True
True
$file = get-content -raw file.txt
$pattern = '(?s)two.{1,2}three.{1,2}four'
# $pattern = 'two\r?\nthree\r?\nfour'
# $pattern = 'two\r\nthree\r\nfour'
# $pattern = 'two\nthree\nfour'
$file -match $pattern
$file | select-string $pattern -quiet