如何在一行中提取2个字符串并使用powershell进行比较?
我有很多txt文件,每个文件都有以下内容。 我想将Run与传递的值进行比较。 e、 g.00001A22与00001A22 我可以使用excel来完成这些工作,但我有200多个文件。 这是一件大事 我尝试使用powershell构建一个脚本来提取2个字符串并进行比较。我试图选择字符串,但失败了。 有什么方法可以工作吗如何在一行中提取2个字符串并使用powershell进行比较?,powershell,Powershell,我有很多txt文件,每个文件都有以下内容。 我想将Run与传递的值进行比较。 e、 g.00001A22与00001A22 我可以使用excel来完成这些工作,但我有200多个文件。 这是一件大事 我尝试使用powershell构建一个脚本来提取2个字符串并进行比较。我试图选择字符串,但失败了。 有什么方法可以工作吗 DRAM Test Run: 00001A22 Passed: 00001A22 Ethernet Test Run: 0
DRAM Test Run: 00001A22 Passed: 00001A22
Ethernet Test Run: 000011E2 Passed: 000011E2
DRAM Test Run: 00001BA7 Passed: 00001BA7
Ethernet Test Run: 000012EC Passed: 000012EC
DRAM Test Run: 00001CA3 Passed: 00001CA3
Ethernet Test Run: 00001399 Passed: 00001399
为此,我将使用带有命名捕获组的正则表达式模式:
# Define pattern that captures the hex strings
$CaptureTestPattern = 'Run: (?<run>[0-9A-F]{8}).*Passed: (?<passed>[0-9A-F]{8})'
# Go through each line in file
Get-Content tests.txt |ForEach-Object {
# Check if string contains both a "run" and "passed" result
if(($m = [regex]::Match($_,$CaptureTestPattern)).Success)
{
# Compare them
if($m.Groups['run'].Value -eq $m.Groups['passed'])
{
# they are the same!
}
else
{
# they are NOT the same!
}
}
}
#定义捕获十六进制字符串的模式
$CaptureTestPattern='运行:(?[0-9A-F]{8})。*通过:(?[0-9A-F]{8})'
#检查文件中的每一行
获取Content tests.txt | ForEach对象{
#检查字符串是否同时包含“运行”和“通过”结果
if($m=[regex]::Match($\u,$CaptureTestPattern)).Success)
{
#比较它们
如果($m.Groups['run'].Value-eq$m.Groups['passed']))
{
#他们是一样的!
}
其他的
{
#他们不一样!
}
}
}
我将使用带有命名捕获组的正则表达式模式:
# Define pattern that captures the hex strings
$CaptureTestPattern = 'Run: (?<run>[0-9A-F]{8}).*Passed: (?<passed>[0-9A-F]{8})'
# Go through each line in file
Get-Content tests.txt |ForEach-Object {
# Check if string contains both a "run" and "passed" result
if(($m = [regex]::Match($_,$CaptureTestPattern)).Success)
{
# Compare them
if($m.Groups['run'].Value -eq $m.Groups['passed'])
{
# they are the same!
}
else
{
# they are NOT the same!
}
}
}
#定义捕获十六进制字符串的模式
$CaptureTestPattern='运行:(?[0-9A-F]{8})。*通过:(?[0-9A-F]{8})'
#检查文件中的每一行
获取Content tests.txt | ForEach对象{
#检查字符串是否同时包含“运行”和“通过”结果
if($m=[regex]::Match($\u,$CaptureTestPattern)).Success)
{
#比较它们
如果($m.Groups['run'].Value-eq$m.Groups['passed']))
{
#他们是一样的!
}
其他的
{
#他们不一样!
}
}
}
根据文件test.txt包含以下内容的事实:
DRAM Test Run: 00001A22 Passed: 00001A22
Ethernet Test Run: 000011E2 Passed: 000011E2
DRAM Test Run: 00001BA7 Passed: 00001BA7
Ethernet Test Run: 000012EC Passed: 000012ED
DRAM Test Run: 00001CA3 Passed: 00001CA3
Ethernet Test Run: 00001399 Passed: 00001399
您可以使用:
Get-Content D:\Temp\test.txt | % {$n=0}{$_ -match '^.*Run: (.*) Passed: (.*)$' | Out-Null; if ($Matches[1] -ne $Matches[2]){"line $n KO"}; $n++}
它回来了
line 3 KO
根据事实,test.txt文件包含以下内容:
DRAM Test Run: 00001A22 Passed: 00001A22
Ethernet Test Run: 000011E2 Passed: 000011E2
DRAM Test Run: 00001BA7 Passed: 00001BA7
Ethernet Test Run: 000012EC Passed: 000012ED
DRAM Test Run: 00001CA3 Passed: 00001CA3
Ethernet Test Run: 00001399 Passed: 00001399
您可以使用:
Get-Content D:\Temp\test.txt | % {$n=0}{$_ -match '^.*Run: (.*) Passed: (.*)$' | Out-Null; if ($Matches[1] -ne $Matches[2]){"line $n KO"}; $n++}
它回来了
line 3 KO
00001A22与00001A22的区别是什么?您正在尝试搜索文件中的字符串吗?00001A22与00001A22的区别是什么?你想在文件中搜索字符串吗?我也使用正则表达式,使用匹配运算符,你的代码比我的一行代码可读性更好。OP想要一行代码tho,但我认为这一行更好。他可以将上述内容封装在一个函数中,并制作自己的“一行代码”;)从技术上讲,我依赖于一条管道,我认为计数为一行我也使用正则表达式,使用匹配运算符,你的代码比我的一行更可读。OP想要一行tho,但我认为这一行更好他可以将上述内容封装在一个函数中,并制作自己的“一行”;)从技术上讲,我依靠的是一条管道,我认为这算是一条班轮