我试图根据InvokeID对以下日志文​​件进行排序。这是最后一列。列虽然没有标题,但我无法根据名称对其进行过滤。我试图用行中数字的位置过滤它,可惜没有找到解决方案

11:20:36:645 ra-agi Trace: Sending Query Request message to application gateway host.     11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359032
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5001  11:20:36:645 ra-agi Trace:     InvokeID =             11359018
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5001  11:20:36:645 ra-agi Trace:     InvokeID =             11359017
11:20:36:645 ra-agi Trace: Sending Query Request message to application gateway host.     11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359033
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359032

最佳答案

日志中没有显示可识别的实际列,并且似乎在值前加上了大量空格。您可以将其视为Fixed-Width表并使用我的ConvertFrom-FixedWidth函数,但是下面的代码应该可以满足您的要求:

Get-Content -Path 'TheLogFile.log' | Sort-Object @{Expression = { [int]($_.Trim() -split '\s+')[-1] }}

结果:
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5001  11:20:36:645 ra-agi Trace:     InvokeID =             11359017
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5001  11:20:36:645 ra-agi Trace:     InvokeID =             11359018
11:20:36:645 ra-agi Trace: Sending Query Request message to application gateway host.     11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359032
11:20:36:645 ra-agi Trace: Received Query Confirm message from application gateway host.  11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359032
11:20:36:645 ra-agi Trace: Sending Query Request message to application gateway host.     11:20:36:645 ra-agi Trace:     ApplicationGatewayID = 5000  11:20:36:645 ra-agi Trace:     InvokeID =             11359033

关于powershell - 根据Powershell中的列对文本文件进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60037083/

10-13 07:46
查看更多