Powershell 如何选择字符串并仅输出最后一行文本
无论如何,我可以让我的输出只搜索我搜索的每个字符串的最后一行Powershell 如何选择字符串并仅输出最后一行文本,powershell,Powershell,无论如何,我可以让我的输出只搜索我搜索的每个字符串的最后一行 $String1 = "Oatmeal" $String2 = "Cake" $String1Location = "C:\1.txt" try { Select-String -Path $String1Location -Pattern $String1, $String2 -ErrorAction Stop } catch { return "" } 我的输出是这样的: C:\1.txt:4:Oatmea
$String1 = "Oatmeal"
$String2 = "Cake"
$String1Location = "C:\1.txt"
try {
Select-String -Path $String1Location -Pattern $String1, $String2 -ErrorAction Stop
}
catch {
return ""
}
我的输出是这样的:
C:\1.txt:4:Oatmeal
C:\1.txt:6:Cake
C:\1.txt:9:Cake
C:\1.txt:10:Oatmeal
但我只想看看
C:\1.txt:9:Cake
C:\1.txt:10:Oatmeal
提前感谢。如果您在try块中执行以下操作如何
$found1=Select-String -Path $String1Location -Pattern $String1 -ErrorAction Stop
$found1[-1]
$found2=Select-String -Path $String1Location -Pattern $String2 -ErrorAction Stop
$found2[-1]
如果您在try块中执行以下操作如何
$found1=Select-String -Path $String1Location -Pattern $String1 -ErrorAction Stop
$found1[-1]
$found2=Select-String -Path $String1Location -Pattern $String2 -ErrorAction Stop
$found2[-1]
这就是我的结局,它正在发挥作用:
try {
Select-String -Path $String1Location -Pattern $String1 -ErrorAction Stop | Where-Object {$_ -match $String1} | Select-Object -Last 1
Select-String -Path $String1Location -Pattern $String2 -ErrorAction Stop | Where-Object {$_ -match $String2} | Select-Object -Last 1
}
这就是我的结局,它正在发挥作用:
try {
Select-String -Path $String1Location -Pattern $String1 -ErrorAction Stop | Where-Object {$_ -match $String1} | Select-Object -Last 1
Select-String -Path $String1Location -Pattern $String2 -ErrorAction Stop | Where-Object {$_ -match $String2} | Select-Object -Last 1
}
我建议:
$String1 = "Oatmeal"
$String2 = "Cake"
$String1Location = "C:\1.txt"
Select-String -Path $String1Location -Pattern $string1, $string2 -SimpleMatch | Group-Object Line | ForEach-Object {
$_.Group | Sort-Object LineNumber | Select-Object -Last 1
}
或者使用正则表达式匹配
$StringsToSearch = "Oatmeal","Cake"
$String1Location = "C:\1.txt"
# build the regex
$pattern = '\b(' + (($StringsToSearch | ForEach-Object { [regex]::Escape($_) }) -join '|') + ')\b'
# this ends up in '\b(Oatmeal|Cake)\b' which means we are looking for these strings as whole string.
# if you also would like to search for strings that contain the search criteria, like 'Pancake',
# then use
# $pattern = ($StringsToSearch | ForEach-Object { [regex]::Escape($_) }) -join '|'
Select-String -Path $String1Location -Pattern $pattern | Group-Object Line | ForEach-Object {
$_.Group | Sort-Object LineNumber | Select-Object -Last 1
}
注意:如果希望输出按行号升序排序,只需在最后一个右括号后添加| Sort Object LineNumber
。我建议:
$String1 = "Oatmeal"
$String2 = "Cake"
$String1Location = "C:\1.txt"
Select-String -Path $String1Location -Pattern $string1, $string2 -SimpleMatch | Group-Object Line | ForEach-Object {
$_.Group | Sort-Object LineNumber | Select-Object -Last 1
}
或者使用正则表达式匹配
$StringsToSearch = "Oatmeal","Cake"
$String1Location = "C:\1.txt"
# build the regex
$pattern = '\b(' + (($StringsToSearch | ForEach-Object { [regex]::Escape($_) }) -join '|') + ')\b'
# this ends up in '\b(Oatmeal|Cake)\b' which means we are looking for these strings as whole string.
# if you also would like to search for strings that contain the search criteria, like 'Pancake',
# then use
# $pattern = ($StringsToSearch | ForEach-Object { [regex]::Escape($_) }) -join '|'
Select-String -Path $String1Location -Pattern $pattern | Group-Object Line | ForEach-Object {
$_.Group | Sort-Object LineNumber | Select-Object -Last 1
}
注意:如果希望输出按行号升序排序,只需在最后一个右括号后添加
| Sort Object LineNumber
。最简单的解决方案是IMO追加| Sort Object Pattern-Unique
如有必要,对对象行号进行排序,使其具有原始顺序
$String1 = "Oatmeal"
$String2 = "Cake"
$String1Location = "C:\1.txt"
try {
Select-String -Path $String1Location -Pattern $String1, $String2 -ErrorAction Stop |
Sort-Object Pattern -Unique | Sort-Object LineNumber
}
catch {
return ""
}
最简单的解决方案是IMO附加
|排序对象模式-唯一
如有必要,对对象行号进行排序,使其具有原始顺序
$String1 = "Oatmeal"
$String2 = "Cake"
$String1Location = "C:\1.txt"
try {
Select-String -Path $String1Location -Pattern $String1, $String2 -ErrorAction Stop |
Sort-Object Pattern -Unique | Sort-Object LineNumber
}
catch {
return ""
}
这是输出:C:\1.txt:4:燕麦C:\1.txt:6:Cake C:\1.txt:9:Cake C:\1.txt:10:Oatmeat C:\1.txt:10:Oatmeat C:\1.txt:9:Caken我不确定我理解你的意思。您是否在try块中尝试了上述代码?这是输出:C:\1.txt:4:Oatmeat C:\1.txt:6:Cake C:\1.txt:9:Cake C:\1.txt:10:Oatmeat C:\1.txt:10:Oatmeat C:\1.txt:9:Caken我不确定是否理解您的意思。您是否在try块中尝试了上述代码?这里的行就是模式,如果模式只是行的一部分,您的方法将不起作用。因此,最好按模式分组。这里的线条就是模式,如果模式只是线条的一部分,那么你的方法就行不通了。所以最好按模式分组。