如何使用正则表达式提取信息

时间:2020-07-07 16:23:30

标签: regex powershell

在powershell脚本中使用以下代码从.txt文件中提取数据,而使用正则表达式无法获得预期的结果:

$PSRoot      = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

$csvContents = @()

Get-Content C:\PROJECTS\Reports\NodeReports\NU_001_20200620.txt |
?{$_ -imatch 'Client Name' -or $_ -imatch 'Windows Ver:'} | %{

if($_ -imatch 'Client Name'){       
           $Name = ([regex]::Matches($_,'\b\d+') | select value).value -join ','
           
                                
           }

if($_ -imatch 'Windows Ver:'){       
           $WinVer = ([regex]::Matches($_,'\b\d+') | select value).value -join ','
                                                      
           }
    $obj = [PSCustomObject]@{
        POSType        = $Name
        WindowsVersion = $WinVer
            
        }

        $csvContents += $obj
                
    }
 $csvContents | ConvertTo-Csv -NoTypeInformation  | Set-Content -path "$PSRoot\NodeReportStatus.csv" -force

输出示例:

Test

POSType(客户端名称)没有信息被提取,而WindowsVersion(Windows Ver :)应该仅提取6.1

.txt文件的一部分:

Node 001 Status Report - Report Version 20200505;
Generated 2020-06-20 00:50:56;
=====================================================================;
DEV001 Windows Ver: 6.1, SerialLink Ver: 1464.056, CPU Type: NT;
DEV001 Name: 2723998POS1, ActiveName: 2723998POS1
DEV001 Time Setup: 06/20/2020   00:50:55   GMT +05:00,YES,35,Eastern Standard Time;
DEV001 Uptime: 9 days, 2 hours, 27 mins, 12.69 secs
---------------------------------------------------------------------;
DEV001 GetInfo: Client Name      P1530;

2 个答案:

答案 0 :(得分:1)

您可以尝试:

DEV\d+ Windows Ver: (\d+)\.(\d+), SerialLink Ver: (\d+)\.(\d+)

示例Powershell命令:(您可以根据进一步的要求修改代码)

PS C:\Path\To\MyDesktop> $input_path='C:\Path\To\MyDesktop\test.txt'
PS C:\Path\To\MyDesktop> $output_path='C:\Path\To\MyDesktop\testResult.txt'
PS C:\Path\To\MyDesktop> $regex='DEV\d+ Windows Ver: (\d+)\.(\d+), SerialLink Ver: (\d+)\.(\d+)'
PS C:\Path\To\MyDesktop> select-string -Path $input_path -Pattern $regex -AllMatches | % { "$($_.matches.groups[1]), $($_.matches.groups[2]), $($_.matches.groups[3]), $($_.matches.groups[4])" } > $output_path

样本输出:

Pictorial Representation

答案 1 :(得分:0)

由于一个文件中可能有多个节点状态报告,所以我将使用switch语句进行正则表达式匹配和自定义对象创建。

$csvcontents = switch -regex -file C:\PROJECTS\Reports\NodeReports\NU_001_20200620.txt {
    'Windows Ver: ([\d\.]+)' {
        $Version = $matches.1
    }
    'Client Name\s*(.*);' {
        $Client = $matches.1
        [pscustomobject]@{POSType = $Client; WindowsVersion = $Version }
    }
}

使用switch和正则表达式,每个匹配的字符串都存储在自动$matches变量中。由于每次匹配仅将一组括号用作目标字符串,因此该字符串成为未命名的捕获组1。可以使用1访问捕获组$matches.1