获取内容中的Powershell多重匹配
我想看看是否有任何行包含单词“更新”和/或“新文件”。我试过下面这样的脚本,但运气不好。如有任何见解,将不胜感激获取内容中的Powershell多重匹配,powershell,robocopy,Powershell,Robocopy,我想看看是否有任何行包含单词“更新”和/或“新文件”。我试过下面这样的脚本,但运气不好。如有任何见解,将不胜感激 RoboCopy.exe "C:\Folder" "C:\Folder2" *.* /S /E /MIR /MT:8 /X /V /TS /FP /BYTES /ETA /LOG:C:\Output\log\sync.txt $SourceDir = "C:\Output\log\" #$GCI_Fiter = '*.txt' $Include=@("*.txt") $File
RoboCopy.exe "C:\Folder" "C:\Folder2" *.* /S /E /MIR /MT:8 /X /V /TS /FP /BYTES /ETA /LOG:C:\Output\log\sync.txt
$SourceDir = "C:\Output\log\"
#$GCI_Fiter = '*.txt'
$Include=@("*.txt")
$FileList = Get-ChildItem -LiteralPath $SourceDir -Include "$Include" -File
foreach ($FL_Item in $FileList) {
$results = Get-Content -Path $FL_Item.FullName | Select-String -pattern '(newer.New File)'
}
机器人复制日志:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wednesday, December 25, 2019 8:16:28 PM
Source : C:\Folder\
Dest : C:\Folder2\
Files : *.*
Options : *.* /V /X /TS /FP /BYTES /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /ETA /MT:8 /R:1000000 /W:30
------------------------------------------------------------------------------
New File 1 2019/12/25 17:16:04 C:\Folder\2.txt
same 0 2019/12/25 11:38:23 C:\Folder\2\1.txt
same 24 2019/09/25 08:42:17 C:\Folder\5.csv
Newer 1 2019/12/25 17:16:04 C:\Folder\4.txt
100%
same 0 2019/12/11 08:06:39 C:\Folder\1.txt
same 0 2019/12/11 08:06:39 C:\Folder\2.txt
------------------------------------------------------------------------------
如果使用
获取内容
将该文件加载到$InStuff
中,则可以使用-match
在集合上工作的方式。这将为您提供与正则表达式模式匹配的行。像这样
$InStuff -match 'newer|new file'
输出
New File 1 2019/12/25 17:16:04 C:\Folder\2.txt
Newer 1 2019/12/25 17:16:04 C:\Folder\4.txt
如果在行首有*.txt文件具有较新的或新文件,则返回$true(这样它也不会显示文件名中可能包含较新的)的误报。) 希望能有所帮助。因为的
-Path
参数采用数组,所以不需要循环。如果我们使用,您可以执行以下操作:
$FileList = Get-ChildItem -LiteralPath $SourceDir -Include $Include -File
$results = Select-String -Path $FileList -Pattern '^\s+(Newer|New File)'
说明:
如果找到任何匹配项,$results
将包含MatchInfo
对象。这些结果的默认显示将是具有匹配项的文件的相对路径、匹配项的行号和匹配行。但是,您可以访问属性以轻松地以不同的方式调整输出。有关如何查看可用属性的信息,请参见下文:
$results[0] | Get-Member -MemberType Property
TypeName: Microsoft.PowerShell.Commands.MatchInfo
Name MemberType Definition
---- ---------- ----------
Context Property Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename Property string Filename {get;}
IgnoreCase Property bool IgnoreCase {get;set;}
Line Property string Line {get;set;}
LineNumber Property int LineNumber {get;set;}
Matches Property System.Text.RegularExpressions.Match[] Matches {get;set;}
Path Property string Path {get;set;}
Pattern Property string Pattern {get;set;}
# List Unique Files That Contain a Match
$results.Path | Get-Unique
# List All File Paths and Their Matching Lines
$results | Select-Object Path,Line
# Default Display
# Format Is RelativePath:LineNumber:Line
$results
# Calculated Properties Including Log File Path, Newer or New File as Type, the Copied File
$results | select Path,
@{n='Type';e={$_.Matches.Groups[1]}},
@{n='Copied File';e={$_.Line -replace '.*\d+:\d+:\d+\s+'}}
有关如何使用属性的示例,请参见下文:
$results[0] | Get-Member -MemberType Property
TypeName: Microsoft.PowerShell.Commands.MatchInfo
Name MemberType Definition
---- ---------- ----------
Context Property Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename Property string Filename {get;}
IgnoreCase Property bool IgnoreCase {get;set;}
Line Property string Line {get;set;}
LineNumber Property int LineNumber {get;set;}
Matches Property System.Text.RegularExpressions.Match[] Matches {get;set;}
Path Property string Path {get;set;}
Pattern Property string Pattern {get;set;}
# List Unique Files That Contain a Match
$results.Path | Get-Unique
# List All File Paths and Their Matching Lines
$results | Select-Object Path,Line
# Default Display
# Format Is RelativePath:LineNumber:Line
$results
# Calculated Properties Including Log File Path, Newer or New File as Type, the Copied File
$results | select Path,
@{n='Type';e={$_.Matches.Groups[1]}},
@{n='Copied File';e={$_.Line -replace '.*\d+:\d+:\d+\s+'}}
正则表达式详细信息:
^\s+(较新的|新文件)
:
:字符串的开头,表示此处每行的开头^
:\s+
是一个空白字符<代码>+是一个或多个匹配项。这表示一个或多个连续的空白\s
:(更新的|新文件)
是regex|
断言。这将匹配较新的或
或
新文件。使用
确保先匹配上一个()
。如果没有^\s+
,则应用()
^\s+较新的
或
新文件。但它确实创建了一个不必要的捕获组
是非捕获版本(?:更新的|新文件)
'newer','new file' | select-string newer,'new file'
newer
new file
谢谢,但我不懂你的正则表达式。请您澄清一下好吗?@Arbelac,
^
表示一行的开头,\s
是一个空格字符,+
是一次或多次(空格无限重复),然后是字符串较新的或新文件
。|
表示或。()
只是一种分组机制,允许在()
中检查|
的整个左侧(LHS)和|
的整个右侧。谢谢@AdminOfThings@Arbelac,我假设这是您在使用(更新的.New文件)
时的意图?