我提取包含大量文本以及 MAC 地址和 UUID 的字符串。 例如:
![LOG[AA:AA:AA:AA:AA:AA, 0A0A0000-0000-0000-0000-A0A00A000000: found optional advertisement C0420054]LOG]!><time="09:07:57.573-120" date="04-19-2017" component="SMSPXE" context="" type="1" thread="2900" file="database.cpp:533"
我想剥离输出以仅显示 MAC 地址(例如 AA:AA:AA:AA:AA:AA
)和 UUID(例如 0A0A0000-0000-0000- 0000-A0A00A000000
)
我不知道如何修剪输出。
这是我的脚本:
$Path = "\\AAAAAAAA\logs$"
$Text = "AA:AA:AA:AA:AA:AA"
$PathArray = @()
$Results = "C:\temp\test.txt"
# This code snippet gets all the files in $Path that end in ".txt".
Get-ChildItem $Path -Filter "*.log" |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}
get-content $PathArray -ReadCount 1000 |
foreach { $_ -match $Text}
最佳答案
您可以使用 Get-ChildItem
cmdlet 的 -Filter
开关,而不是使用 Where-Object
cmdlet 来过滤所有文件.此外,您不必自己使用 Get-content
cmdlet 加载内容,只需将文件通过管道传输到 Select-String
cmdlet。
为了获取 MAC 和 UUID,我用谷歌搜索了两个 regex 并将它们组合起来:
$Path = "\\AAAAAAAA\logs$"
$Pattern = '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}),\s+(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})'
$Results = "C:\temp\test.txt"
Get-ChildItem $Path -Filter "*.log" -File |
Select-String $Pattern |
ForEach-Object {
$_.Matches.Value
} |
Out-File $Results
关于regex - 从字符串中提取 MAC 地址和 UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43490347/