regex - 从字符串中提取 MAC 地址和 UUID

标签 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}

最佳答案

您可以使用 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/

相关文章:

Powershell TrimEnd 修剪尾随 e

windows - 我可以在 Windows 下向事件进程的 STDIN 发送一些文本吗?

powershell - 如何编写中间带有参数的 PowerShell 别名?

powershell - 递归计数文件夹树的文件

php - PHP中的正则表达式过滤@name或@name.lastname

Javascript Regex - 伪造后视的意外行为

javascript - 我将使用什么 JavaScript 正则表达式来发现一个字符串包含一个开放的 HTML 标记但不包含一个关闭的 HTML 标记?

java - 正则表达式在多行模式下不匹配空字符串 (Java)

Java - 正则表达式 - 捕获 ""之间的组

powershell - 如何使用PowerShell一次加载和处理文件