从文件中提取数据并使用powershell将其分类到新文件中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从文件中提取数据并使用powershell将其分类到新文件中相关的知识,希望对你有一定的参考价值。
我正在编写一个脚本,该脚本应该为我提供每次调用ID的时间。短行包含调用ID。在长行中,我收到服务器的消息。
我需要通过获取ID和ApplicationGatewayID以及发送和接收之间的时间差来构造文件。
我的计划是在一行上获得请求和响应,尽管该文件是按invokeID排序的,但模式不是规则的。此外,有一些ID仅具有响应而没有请求,这使它更难。
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:739 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:739 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:739 ra-agi Trace: InvokeID = 11359026
11:20:36:723 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:723 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:723 ra-agi Trace: InvokeID = 11359027
11:20:36:739 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:739 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:739 ra-agi Trace: InvokeID = 11359028
11:20:36:739 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:739 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:739 ra-agi Trace: InvokeID = 11359029
11:20:36:848 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:848 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:848 ra-agi Trace: InvokeID = 11359031
11:20:36:645 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 11359033
11:20:36:676 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:676 ra-agi Trace: ApplicationGatewayID = 5000 11:20:36:676 ra-agi Trace: InvokeID = 11359033
11:20:36:848 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:848 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:848 ra-agi Trace: InvokeID = 11359034
11:20:36:645 11359034
11:20:36:676 11359035
11:20:36:848 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:848 ra-agi Trace: ApplicationGatewayID = 5001 11:20:36:848 ra-agi Trace: InvokeID = 11359035
11:20:36:739 ra-agi Trace: Received Query Confirm message from application gateway host. 11:20:36:739 ra-agi Trace: ApplicationGatewayID = 5000 11:20:36:739 ra-agi Trace: InvokeID = 11359036
11:20:36:739 11359036
11:20:36:739 11359037
最终产品应如下所示:第一列InvokeID,第二列ApplicationGatewayID,第三列:发送和接收之间的时间
11359017 5001 127
11359018 5000 114
11359019 5001 105
答案
这应该起作用:
Get-Content 'D:\myfile.txt' | ? $_ -like '*InvokeID*' | ForEach-Object
$a = @($_ -split ' +')
$d = New-TimeSpan -Start ([System.DateTime](($a[0]).Substring(0,8))) -End ([System.DateTime](($a[11]).Substring(0,8)))
"$($a[22]) $($a[16]) $d"
另一答案
看到您的最后一条评论,如果我正确理解了这个问题,应该这样做:
$result = (Get-Content -Path 'X:\TheInputFile.txt').Trim() |
Group-Object @Expression = $_.Substring($_.Length - 8) | # group by InvokeID (last 8 characters of the trimmed line)
Where-Object $_.Count -eq 2 | # select only groups with two items in it (one long and one short line)
ForEach-Object
$invokeID = $_.Name
$millisOne = $millisTwo = 0 # two variables to store the parsed milliseconds from the time stamps
foreach ($line in $_.Group)
$timeToParse = $line.Substring(0,12) -replace ':(\d3$)', '.$1' # replace the last colon ':' into a dot '.' for parsing
if ($line -match 'ApplicationGatewayID\s+=\s+(\d+)') # if this is the long line..
$gatewayID = $Matches[1]
$millisOne = [TimeSpan]::Parse($timeToParse).TotalMilliSeconds
else
$millisTwo = [TimeSpan]::Parse($timeToParse).TotalMilliSeconds
# output an object with the properties you need
[PsCustomObject]@
'InvokeID' = $invokeID
'ApplicationGatewayID' = $gatewayID
'ProcessTime' = [Math]::Abs($millisOne - $millisTwo)
# output the result on screen
$result
# write the resukt to a new CSV file
$result | Export-Csv -Path 'X:\TheOutputFile.csv' -NoTypeInformation
使用示例输入文件将产生以下结果:
InvokeID ApplicationGatewayID ProcessTime -------- -------------------- ----------- 11359032 5000 0 11359033 5000 31 11359034 5001 203 11359035 5001 172 11359036 5000 0
以上是关于从文件中提取数据并使用powershell将其分类到新文件中的主要内容,如果未能解决你的问题,请参考以下文章
powershell 使用SQL文件和PowerShell从数据库中提取数据。设计用于SQL服务器并以CSV格式输出。看到这个链接:h
使用 Powershell 从 PostgreSQL 检索数据