[英]How do I extract information using regex
Using the following code in a powershell script to pull data from a.txt file and not getting desired results using regex:在 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
Example of output: output 示例:
No info is being pulled for POSType (Client Name) and WindowsVersion (Windows Ver:) should just be pulling 6.1没有为POSType(客户端名称)和 WindowsVersion(Windows Ver:)提取信息,应该只是拉 6.1
Portion of.txt file: .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;
You may try:你可以试试:
DEV\d+ Windows Ver: (\d+)\.(\d+), SerialLink Ver: (\d+)\.(\d+)
Sample Powershell Commands:(You can modify your code according to your further requirement)示例 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
Since you could have multiple Node Status Reports within one file, I'd use a switch
statement to do regex matching and custom object creation.由于您可以在一个文件中有多个节点状态报告,因此我将使用switch
语句进行正则表达式匹配和自定义 object 创建。
$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 }
}
}
Using switch
with regex, each matching string is stored in the automatic $matches
variable.使用带有正则表达式的switch
,每个匹配的字符串都存储在自动$matches
变量中。 Since we are only using one set of parentheses per match for the target string, that becomes unnamed capture group 1
.由于我们对目标字符串的每个匹配项只使用一组括号,因此它成为未命名的捕获组1
。 Capture group 1
can be accessed using $matches.1
.可以使用$matches.1
访问捕获组1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.