从字符串

时间:2017-04-19 08:26:55

标签: regex powershell

我提取包含大量文本以及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}

1 个答案:

答案 0 :(得分:5)

您可以使用Where-Object cmdlet的-Filter开关,而不是使用Get-ChildItem cmdlet过滤所有文件。您也不必自己使用Get-content cmdlet加载内容,只需将文件传输到Select-String cmdlet即可。

要抓取MAC,UUID我只是使用正则表达式搜索并合并它们:

$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