0

I got a file with the following pattern:

10:15:16:290 53123
10:15:16:290 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:290 ra-agi Trace:     ApplicationGatewayID = 5001  10:15:16:290 ra-agi Trace:     InvokeID =             5456787 
10:15:16:493 5456787
10:15:16:306 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:306 ra-agi Trace:     ApplicationGatewayID = 5000  10:15:16:306 ra-agi Trace:     InvokeID =             132 
10:15:16:337 132
10:15:16:509 54565
10:15:16:337 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:337 ra-agi Trace:     ApplicationGatewayID = 5001  10:15:16:337 ra-agi Trace:     InvokeID =             54565 
10:15:16:400 5456512
10:15:16:384 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:384 ra-agi Trace:     ApplicationGatewayID = 5000  10:15:16:384 ra-agi Trace:     InvokeID =             5456512 
10:15:16:603 5
10:15:16:400 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:400 ra-agi Trace:     ApplicationGatewayID = 5001  10:15:16:400 ra-agi Trace:     InvokeID =             5 
10:15:16:493 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:493 ra-agi Trace:     ApplicationGatewayID = 5000  10:15:16:493 ra-agi Trace:     InvokeID =             3124 
10:15:16:509 3124
10:15:16:509 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:509 ra-agi Trace:     ApplicationGatewayID = 5000  10:15:16:509 ra-agi Trace:     InvokeID =             95787812 
10:15:16:525 95787812
10:15:16:509 ra-agi Trace: Sending Query Request message to application gateway host.     10:15:16:509 ra-agi Trace:     ApplicationGatewayID = 5001  10:15:16:509 ra-agi Trace:     InvokeID =             9578781 
10:15:16:728 9578781
10:15:16:712 62

With my Code I'm trying to sort the short lines by the number with following code:

$result = [System.IO.File]::ReadLines($file).Trim() |
Group-Object @{Expression = { [int] $_.Substring($_.Length - 8)} } | # group by InvokeID (last 8 characters of the shortline) 
Where-Object { $_.Count -eq 2 } | # select only groups with two items in it (one long and one short line)

My problem is that the numbers do not always have the same length. How can I make it dynamic?. I got numbers from 1 digit to 8 digits.

mklement0
  • 245,023
  • 45
  • 419
  • 492
fbe106360
  • 61
  • 5

1 Answers1

2

Use the -replace operator with a regular expression to remove everything but trailing consecutive digits:

... |Group-Object { ($_ -replace '^.*?(\d+)$', '$1') -as [int] }
Mathias R. Jessen
  • 106,010
  • 8
  • 112
  • 163