使用拆分作为分隔符
Use split in as delimiter
我在一个位置有很多 .txt 文件。此txt内容如下
%-ile | Read (ms) | Write (ms) | Total (ms)
----------------------------------------------
min | N/A | 0.018 | 0.018
25th | N/A | 0.055 | 0.055
50th | N/A | 0.059 | 0.059
75th | N/A | 0.062 | 0.062
90th | N/A | 0.070 | 0.070
95th | N/A | 0.073 | 0.073
99th | N/A | 0.094 | 0.094
3-nines | N/A | 0.959 | 0.959
4-nines | N/A | 67.552 | 67.552
5-nines | N/A | 75.349 | 75.349
6-nines | N/A | 84.994 | 84.994
7-nines | N/A | 85.632 | 85.632
我正在阅读上面的内容 3-nines 并且想编写一个类似的程序 Total (ms) column's value in greater比 1 关于 3-nines 行,它应该打印该文件名。
为此,我编写了如下程序:
$data = get-content "*.txt" | Select-String -Pattern "3-nines"
$data | foreach {
$items = $_.split("|")
if ($items[0] -ge 1 ) {Echo $items[1]}
}
但是低于错误。
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'split'.
At line:2 char:18
+ $items = $_.split <<<< ("|")
+ CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Cannot index into a null array.
At line:3 char:12
+ if ($items[ <<<< 0] -lt 1 ) {Echo $items[1]}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
你能帮忙吗?我对 powershell 脚本很陌生。
改变
$items = $_.split("|")
至:
$items = ([string]$_).split("|")
匹配项的内容以数组形式返回,并且没有拆分方法。将其转换为字符串将为您提供 split 方法。
更新: 要打印文件名,您必须稍微更改脚本,因为 Select-String 的当前输入是一个数组,因此您丢失了文件名:
Select-String -Pattern "3-nines" -Path "*.txt" | foreach {
$items = ([string]$_).split("|")
if ([double]$items[3] -ge 1 ) {
Write-Output "FileName: $($_.Filename)"
Echo $items[3]
}
}
首先 - 你为什么要在这里传送到 Select-String
?您可以使用 -Path
参数并将 *.txt
直接传递给它。
split 不起作用的原因是你应该再次调用 [Microsoft.PowerShell.Commands.MatchInfo]
对象的 Line
属性。我想你需要的是一个简单的 Where-Object
:
Select-String -Pattern 3-nines -Path *.txt |
Where-Object { [double]($_.line.Split('|')[-1]) -gt 1 } |
Select-Object Path, Line
或者,您可以使用 Import-Csv
cmdlet 将文件内容转换为对象:
foreach ($file in Get-ChildItem -Path *.txt) {
# Existing headers are terrible - replacing them...
nines = Import-Csv -Path $file.FullName -Delimiter '|' -Header Percent, Read, Write, Total |
Where-Object Percent -match 3-nines
if ([double]nines.Total -gt 1) {
nines | Select-Object *, @{
Name = 'Path'
Expression = { $file.FullName }
}
}
}
我在一个位置有很多 .txt 文件。此txt内容如下
%-ile | Read (ms) | Write (ms) | Total (ms)
----------------------------------------------
min | N/A | 0.018 | 0.018
25th | N/A | 0.055 | 0.055
50th | N/A | 0.059 | 0.059
75th | N/A | 0.062 | 0.062
90th | N/A | 0.070 | 0.070
95th | N/A | 0.073 | 0.073
99th | N/A | 0.094 | 0.094
3-nines | N/A | 0.959 | 0.959
4-nines | N/A | 67.552 | 67.552
5-nines | N/A | 75.349 | 75.349
6-nines | N/A | 84.994 | 84.994
7-nines | N/A | 85.632 | 85.632
我正在阅读上面的内容 3-nines 并且想编写一个类似的程序 Total (ms) column's value in greater比 1 关于 3-nines 行,它应该打印该文件名。 为此,我编写了如下程序:
$data = get-content "*.txt" | Select-String -Pattern "3-nines"
$data | foreach {
$items = $_.split("|")
if ($items[0] -ge 1 ) {Echo $items[1]}
}
但是低于错误。
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'split'.
At line:2 char:18
+ $items = $_.split <<<< ("|")
+ CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Cannot index into a null array.
At line:3 char:12
+ if ($items[ <<<< 0] -lt 1 ) {Echo $items[1]}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
你能帮忙吗?我对 powershell 脚本很陌生。
改变
$items = $_.split("|")
至:
$items = ([string]$_).split("|")
匹配项的内容以数组形式返回,并且没有拆分方法。将其转换为字符串将为您提供 split 方法。
更新: 要打印文件名,您必须稍微更改脚本,因为 Select-String 的当前输入是一个数组,因此您丢失了文件名:
Select-String -Pattern "3-nines" -Path "*.txt" | foreach {
$items = ([string]$_).split("|")
if ([double]$items[3] -ge 1 ) {
Write-Output "FileName: $($_.Filename)"
Echo $items[3]
}
}
首先 - 你为什么要在这里传送到 Select-String
?您可以使用 -Path
参数并将 *.txt
直接传递给它。
split 不起作用的原因是你应该再次调用 [Microsoft.PowerShell.Commands.MatchInfo]
对象的 Line
属性。我想你需要的是一个简单的 Where-Object
:
Select-String -Pattern 3-nines -Path *.txt |
Where-Object { [double]($_.line.Split('|')[-1]) -gt 1 } |
Select-Object Path, Line
或者,您可以使用 Import-Csv
cmdlet 将文件内容转换为对象:
foreach ($file in Get-ChildItem -Path *.txt) {
# Existing headers are terrible - replacing them...
nines = Import-Csv -Path $file.FullName -Delimiter '|' -Header Percent, Read, Write, Total |
Where-Object Percent -match 3-nines
if ([double]nines.Total -gt 1) {
nines | Select-Object *, @{
Name = 'Path'
Expression = { $file.FullName }
}
}
}