Powershell 使用选择字符串从文件中提取表数据

Powershell 使用选择字符串从文件中提取表数据,powershell,Powershell,这是我的示例文本文件,我正在尝试解析结果 2017-08-26 22:31:10,769 - Recv: T:150.01 /150.00 B:59.77 /60.00 @:23 B@:127 2017-08-26 22:31:12,559 - Recv: echo:busy: processing 2017-08-26 22:31:12,768 - Recv: T:150.04 /150.00 B:59.93 /60.00 @:22 B@:127 2017-08-26 22:31:13,6

这是我的示例文本文件,我正在尝试解析结果

2017-08-26 22:31:10,769 - Recv:  T:150.01 /150.00 B:59.77 /60.00 @:23 B@:127
2017-08-26 22:31:12,559 - Recv: echo:busy: processing
2017-08-26 22:31:12,768 - Recv:  T:150.04 /150.00 B:59.93 /60.00 @:22 B@:127
2017-08-26 22:31:13,660 - Recv: Bilinear Leveling Grid:
2017-08-26 22:31:13,665 - Recv:       0      1      2      3      4
2017-08-26 22:31:13,669 - Recv:  0 +0.203 +0.105 -0.020 -0.182 -0.275
2017-08-26 22:31:13,672 - Recv:  1 +0.192 +0.100 -0.028 -0.192 -0.310
2017-08-26 22:31:13,675 - Recv:  2 +0.138 +0.018 -0.090 -0.257 -0.340
2017-08-26 22:31:13,678 - Recv:  3 +0.117 +0.018 -0.087 -0.247 -0.362
2017-08-26 22:31:13,681 - Recv:  4 +0.105 -0.020 -0.122 -0.285 -0.385
我需要搜索和分割内容,使其看起来像这样

      0      1      2      3      4
0 +0.203 +0.105 -0.020 -0.182 -0.275
1 +0.192 +0.100 -0.028 -0.192 -0.310
2 +0.138 +0.018 -0.090 -0.257 -0.340
3 +0.117 +0.018 -0.087 -0.247 -0.362
4 +0.105 -0.020 -0.122 -0.285 -0.385
这是我的尝试

Get-Content \\192.168.1.41\octoprint\logs\serial.log | Select-String "Bilinear Leveling Grid:" -Context 0,6 -SimpleMatch  | %{$_.Line.Split("Recv:  ")}
这是我的输出

2017-08-26
22
31
13,660
-






Bilin
ar
L


ling
Grid
有什么想法吗?

使用-replace操作符怎么样 正则表达式在这里当然很有用,但是
Select String
不是我在这里选择的工具。使用
-replace
操作符,我们可以在两个操作中得到您想要的

# Read in the file as one string. 
$textData = Get-Content "\\192.168.1.41\octoprint\logs\serial.log" | Out-String

# Remove everything up to and including a line with Bilinear Leveling Grid: 
# and remove all the prefixes from remaining lines be deleted everything up until Recv: . 
$textData -replace "(?m)^.*?Bilinear Leveling Grid:\s+$" -replace "(?m)^.*?Recv: "
我们在正则表达式中使用多行修饰符,以便锚定符
^$
匹配文本中行的开始和结束,而不确定文本本身的开始和结束。这样可以更容易地删除全局文本

选择字符串 虽然我觉得我的方法更稳健一些,但我想展示一下如何使用
Select String

$result = $textData | Select-String "Bilinear Leveling Grid:" -Context 0,6 -SimpleMatch 
$result.Context.PostContext -replace "^.*?Recv: "
所以,如果你知道它将始终是6行,将工作以及。在本例中,我们再次使用replace操作符从其余行中删除时间戳等

为什么你的方法不起作用 当您使用context时,生成的MatchInfo对象将其存储在名为
context
的属性中,就像您在上面的解决方案中看到的那样

此参数不会更改“选择字符串”生成的对象数。选择字符串为每个匹配生成一个MatchInfo(Microsoft.PowerShell.Commands.MatchInfo)对象。上下文作为字符串数组存储在对象的context属性中

你没有提到它,这也是你的结果如此的部分原因。因此,我们得到了匹配后的上下文,这就得到了“表标题”后面的6行

另一个问题是,您在匹配的一行上使用了
Split()
,Split会将该行刻在您传递的任何字符上,而不是整个字符串上。考虑<代码>“ABCDE”。拆分(“BD”)< /代码>,它将构成一个3元素数组。 使用-replace操作符怎么样 正则表达式在这里当然很有用,但是
Select String
不是我在这里选择的工具。使用
-replace
操作符,我们可以在两个操作中得到您想要的

# Read in the file as one string. 
$textData = Get-Content "\\192.168.1.41\octoprint\logs\serial.log" | Out-String

# Remove everything up to and including a line with Bilinear Leveling Grid: 
# and remove all the prefixes from remaining lines be deleted everything up until Recv: . 
$textData -replace "(?m)^.*?Bilinear Leveling Grid:\s+$" -replace "(?m)^.*?Recv: "
我们在正则表达式中使用多行修饰符,以便锚定符
^$
匹配文本中行的开始和结束,而不确定文本本身的开始和结束。这样可以更容易地删除全局文本

选择字符串 虽然我觉得我的方法更稳健一些,但我想展示一下如何使用
Select String

$result = $textData | Select-String "Bilinear Leveling Grid:" -Context 0,6 -SimpleMatch 
$result.Context.PostContext -replace "^.*?Recv: "
所以,如果你知道它将始终是6行,将工作以及。在本例中,我们再次使用replace操作符从其余行中删除时间戳等

为什么你的方法不起作用 当您使用context时,生成的MatchInfo对象将其存储在名为
context
的属性中,就像您在上面的解决方案中看到的那样

此参数不会更改“选择字符串”生成的对象数。选择字符串为每个匹配生成一个MatchInfo(Microsoft.PowerShell.Commands.MatchInfo)对象。上下文作为字符串数组存储在对象的context属性中

你没有提到它,这也是你的结果如此的部分原因。因此,我们得到了匹配后的上下文,这就得到了“表标题”后面的6行

另一个问题是,您在匹配的一行上使用了
Split()
,Split会将该行刻在您传递的任何字符上,而不是整个字符串上。考虑<代码>“ABCDE”。拆分(“BD”)< /代码>,它将构成一个3元素数组。 试试这个:

$file="\\192.168.1.41\octoprint\logs\serial.log"
$delimiterrow="Bilinear Leveling Grid:"

Get-Content $file | Select-String $delimiterrow -Context 0,6 |%{ $_.Context.PostContext | %{ ($_ -split 'Recv:  ', 2)[1]}}

#short version
gc $file | sls $delimiterrow -Co 0,6 |%{ $_.Context.PostContext | %{ ($_ -split 'Recv:  ', 2)[1]}}
试试这个:

$file="\\192.168.1.41\octoprint\logs\serial.log"
$delimiterrow="Bilinear Leveling Grid:"

Get-Content $file | Select-String $delimiterrow -Context 0,6 |%{ $_.Context.PostContext | %{ ($_ -split 'Recv:  ', 2)[1]}}

#short version
gc $file | sls $delimiterrow -Co 0,6 |%{ $_.Context.PostContext | %{ ($_ -split 'Recv:  ', 2)[1]}}
因此,数据在“双线性水平网格:”之后开始,您希望后面的所有线都不带前缀?因此,数据在“双线性水平网格:”之后开始,您希望后面的所有线都不带前缀?