【发布时间】:2018-09-18 02:08:48
【问题描述】:
我想说(我是企鹅)我不是 Windows 或 Powershell 人,但这不应该阻止我帮助我们的 Windows 团队。
我需要合并两个日志并按日期和时间对它们进行排序。我认为将它们组合起来应该很简单,但是按日期和时间排序似乎有点让我失望。
我正在使用的日志没有相同数量的列,因此我对日志进行了某种规范化,以尝试按日期和时间的 logline[3,4] 进行排序。
"SMTPD" 4416 2476943 "2018-09-11 23:53:37.410" "1.1.1.1" "SENT: 221 goodbye"
"TCPIP" 4308 "2018-09-11 23:59:47.255" "TCP - 1.1.1.2 connected to 1.1.1.1:25."
"SMTPD" 4308 2476952 "2018-09-11 23:22:47.255" "1.1.1.1" "SENT: 220 mx9.bobdestroyer.com ESMTP"
"SMTPD" 4416 2476952 "2018-09-11 23:35:47.255" "1.2.3.4" "RECEIVED: EHLO smtp-cow-666"
"SMTPD" 4416 2476952 "2018-09-11 23:22:47.255" "1.1.1.1" "SENT: 250-mx5.bobthedestroyer.com[nl]250-SIZE 20480000[nl]250-AUTH LOGIN[nl]250 HELP"
"SMTPD" 4232 2476952 "2018-09-11 23:53:47.255" "1.1.1.1" "RECEIVED: MAIL FROM:<bobtheBuilder@builders.com>"
"SMTPD" 4232 2476952 "2018-09-11 23:59:47.255" "1.1.1.1" "SENT: 250 OK"
"SMTPD" 4416 2476952 "2018-09-11 23:11:47.270" "1.1.1.1" "RECEIVED: RCPT TO:<bobtheBuilder@builders.com>"
"SMTPD" 4416 2476952 "2018-09-11 23:22:47.270" "1.1.1.1" "SENT: 250 OK"
"SMTPD" 4308 2476952 "2018-09-11 23:55:47.270" "1.1.1.1" "RECEIVED: DATA"
"SMTPD" 4308 2476952 "2018-09-11 23:21:47.270" "1.1.1.1" "SENT: 354 OK, send."
"SMTPD" 4000 2476952 "2018-09-11 09:53:48.208" "1.1.1.1" "SENT: 250 Queued (0.768 seconds)"
"APPLICATION" 3100 "2018-09-11 11:53:48.208" "SMTPDeliverer - Message 2570349: Delivering message from bobtheBuilder@builders.com to bobtheDestroyers@Destroyerrs.com . File: C:\Program Files (x86)\servers\toomanysecrets\{49E08D79-C4A5-43F1-9435-9999999999}.eml"
"APPLICATION" 3100 "2018-09-11 12:12:48.208" "SMTPDeliverer - Message 2570349: Relaying to host bobtheBuilder@builders.com ."
这是我写的:
$Unclean_LogLines = Get-Content .\BHmailLog.txt
#$LogLines | %{"$($_.Split()[0,1,2,3,4,5,6,7,8,9,10,11,12,13 ])"}
$AppendedLogLines = [System.Collections.ArrayList]@()
#Attempts to normalise the log.... And even out the columns.So that I can grap $_[3,4] for each line.
#perhaps a simple foreach + regex would be better....
$Unclean_LogLines | foreach-object {
$firstcolumn = ($_ -split '\s+',4)[0]
if($firstcolumn -eq '"APPLICATION"'){
$_ = '"APPLICATION" ' + $_
$AppendedLogLines.Add($_ + "`n")
}
elseif($firstcolumn -eq '"TCPIP"'){
$_ = '"TCPIP" ' + $_
$AppendedLogLines.Add($_ + "`n") # minor problem here. I am not 100% normalising the log... I should make _$[2] = 4248 or something.
}
else{
$AppendedLogLines.Add($_ + "`n")
}
}
"FINISHED NORMALISING!! "
$AppendedLogLines| foreach-object {
$timestamp,$null = %{"$($_.Split()[3,4])"}
$timestamp = $timestamp.Replace('"','') # remove the last qoate....
$_ |sort-object -property {
}
【问题讨论】:
-
日期和时间是否总是从相同的列号开始并占据相同的列位置数?
-
它没有。出于这个原因,我使用代码在那些没有足够列的行中附加字段。 $AppendedLogLines 列在索引 (3,4) 处具有此日期和时间
-
标准化以匹配
"SMTPD"行,然后将其写为 CSV 文件。然后,使用Import-CSV(链接上的文档)导入,并使用[DateTime]::ParseExact()处理日期/时间字段(请参阅TechNet 和this SO question -
记住,PowerShell 喜欢 objects,而不是 text。它提供了将文本转换为对象的方法,而无需手动进行繁重的解析,就像在
bash或perl中所做的那样。 -
您上面的示例日志段是否显示了您可能遇到的所有行格式?
标签: powershell sorting datetime