根据Powershell中的列对txt文件进行排序

时间:2020-02-03 10:12:35

标签: powershell

我正在尝试根据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 

1 个答案:

答案 0 :(得分:4)

该日志未显示任何实际可识别的列,并且似乎在值前加上了大量空格。您可以将其视为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
相关问题