选择字符串-将匹配项复制到新的文本文件时,其输出顺序不同

时间:2018-09-26 15:38:52

标签: powershell

过去,我一直在Windows PowerShell ISE中使用以下内容搜索包含大量文本文件的文件夹,查找包含指定文本的行并将这些行返回到新的文本文件中。

Param (
       [Parameter(Mandatory=$true)] 
          [string] $FullyQualifiedPath
)


$OutputFile = "Output File Pathway"
$FullyQualifiedPath = "Search Files Pathway"
$Pat1 = [regex] 'Search For This Text'

Remove-Item "$OutputFile"
Get-ChildItem -Path "$FullyQualifiedPath" |
Get-Content  | Select-String -Pattern $Pat1 -AllMatches  >> "$OutputFile"

到目前为止,它运行良好。但是,我现在为获得相同的效果但具有多种模式而感到困惑。

我尝试了以下操作:

Param (
       [Parameter(Mandatory=$true)] 
          [string] $FullyQualifiedPath
)


$OutputFile = "Output File Pathway"
$FullyQualifiedPath = "Search Files Pathway"
$Pat1 = [regex] 'Search For This Text'
$Pat2 = [regex] 'Search For This Text As Well"

Remove-Item "$OutputFile"
Get-ChildItem -Path "$FullyQualifiedPath" |
Get-Content  | Select-String -Pattern $Pat1 -AllMatches  >> "$OutputFile"
Get-Content  | Select-String -Pattern $Pat2 -AllMatches  >> "$OutputFile"

但这返回:

Search For This Text  
Search For This Text  
Search For This Text  
Search For This Text As Well  
Search For This Text As Well  
Search For This Text As Well  

与我的要求相反:

Search For This Text  
Search For This Text As Well  
Search For This Text  
Search For This Text As Well  
Search For This Text  
Search For This Text As Well  

反正我可以根据需要将其输出吗?

我对这种语言完全不熟悉,原始代码来自在线资源。任何帮助,将不胜感激!

0 个答案:

没有答案
相关问题