0

Here is my sample text file, I'm trying to parse out the results

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

I need to search and split the content so it looks like this

      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

Here is my attempt

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

And here is my output

2017-08-26
22
31
13,660
-






Bilin
ar
L


ling
Grid

Any ideas?

1
  • So the data starts after "Bilinear Leveling Grid:" and you want all the lines after that but without the prefix? Commented Aug 27, 2017 at 0:46

2 Answers 2

3

How about just using the -replace operator

Regex is certainly useful here but Select-String would not have been my tool of choice here. Using the -replace operator we can get what you want in two operations.

# 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: "

We use the multiline modifier in regex so that the anchors ^$ match the start and end of the lines in the text and not sure the start and the end of the text itself. This makes it easier to remove globs of text.

Select-String

While I feel my approach is a little more robust I wanted to show how you could have gotten the results you wanted with Select-String

$result = $textData | Select-String "Bilinear Leveling Grid:" -Context 0,6 -SimpleMatch 
$result.Context.PostContext -replace "^.*?Recv: "

So if you knew it would always be 6 lines that would work as well. Again in this example we use the replace operator to removing the timestamps etc. from the remaining lines.

Why your approach was not working

When you use context the resulting MatchInfo object stores it in a property called Context like you saw in my above solution. From the docs about Select-String and -Context

This parameter does not change the number of objects generated by Select-String. Select-String generates one MatchInfo (Microsoft.PowerShell.Commands.MatchInfo) object for each match. The context is stored as an array of strings in the Context property of the object.

You were not referring to it which is part of the reason your results are the way they are. So we get the context after the match and that gets you the 6 lines following the "table title".

Your other issue is you were using Split() on the one line you matched and split will carve the line on any character you pass it and not on the whole string. Consider "abcde".Split("bd") which will make a 3 element array.

Sign up to request clarification or add additional context in comments.

Comments

0

try this:

$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]}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.