【发布时间】:2015-06-04 01:16:50
【问题描述】:
我正在尝试使用 AWK(或 SED 或两者的组合)来解析包含特定字符串“Info:AgentSession”的日志文件。
我想包含包含 START 字符串“Info:AgentSession”的行,但不包含 END 字符串行,即“[2015-”。
这是 CentOS 服务器上文本日志文件的 sn-p:
[2015-03-30 12:23:10.999] [124] [Info:AgentSession] Handling Agent message for PieraC
Request: ReceiveReady
Action: DoNotDisturb
[2015-03-30 12:23:11.000] [124] [Info:AgentSession] Sending agent message to PieraC
Response: ReceiveReady
RequestId:
Status: Ok
Message:
IsReady: False
[2015-03-30 12:23:11.000] [49] [Info:Database] (BZ2411) (SqlTaskWorker.ProcessTasks) Attempting to run task. Thread: SqlTaskWorker-37. StartTime: 1/1/0001 12:00:00 AM. ConnectionTimeout: 15. ConnectionState: Open.
[2015-03-30 12:23:11.501] [111] [Info:Dialer] Sending Dialer message
Action: UsmCommand
Command: Transfer
IsTransfered: False
[2015-03-30 12:23:11.502] [111] [Info:AgentSession] Sending agent message to MatthewW
ActivityState: Wrapup
IsReady: False
IsSipRegistered: True
[2015-03-30 12:23:11.502] [79] [Info:Database] (BZ2411) (SqlTask.Execute) Attempting to start. Thread: SqlTaskWorker-67.
[2015-03-30 12:23:16.207] [124] [Info:AgentSession] Sending agent message to PieraC
Response: NonQuery
Status: Ok
Message: Query sent successfully
[2015-03-30 12:23:16.207] [88] [Info:Database] (BZ2411) (SqlTaskWorker.ProcessTasks) Attempting to run task. Thread: SqlTaskWorker-76.
[2015-03-30 12:23:16.207] [88] [Info:Database] (BZ2411) (SqlTask.Execute) Attempting to start. Thread: SqlTaskWorker-76.
[2015-03-30 12:23:16.208] [88] [Info:Database] (BZ2411) (SqlNonQueryTask.ExecuteCommand) Attempting to start. Thread: SqlTaskWorker-76.
[2015-03-30 12:23:16.268] [124] [Info:AgentSession] Handling Agent message for PieraC
Request: CallAction
CallDisposition:
当我运行以下命令时:
awk '/Info:AgentSession/ {flag=1;next} /\[2015-/{flag=0} flag {print}' test.log
我得到以下输出:
Request: ReceiveReady
Action: DoNotDisturb
Response: ReceiveReady
RequestId:
Status: Ok
Message:
IsReady: False
ActivityState: Wrapup
IsReady: False
IsSipRegistered: True
Response: NonQuery
Status: Ok
Message: Query sent successfully
Request: CallAction
CallDisposition:
但我希望这个输出包括“Info:AgentSession”的 START 字符串,所以实际上最终看起来像这样(省略不引用 的日志的所有其他部分START 字符串,使用 DATE 字符串“[2015-” 的开头作为 END 字符串):
[2015-03-30 12:23:10.999] [124] [Info:AgentSession] Handling Agent message for PieraC
Request: ReceiveReady
Action: DoNotDisturb
[2015-03-30 12:23:11.000] [124] [Info:AgentSession] Sending agent message to PieraC
Response: ReceiveReady
RequestId:
Status: Ok
Message:
IsReady: False
[2015-03-30 12:23:11.502] [111] [Info:AgentSession] Sending agent message to MatthewW
ActivityState: Wrapup
IsReady: False
IsSipRegistered: True
[2015-03-30 12:23:16.207] [124] [Info:AgentSession] Sending agent message to PieraC
Response: NonQuery
Status: Ok
Message: Query sent successfully
[2015-03-30 12:23:16.268] [124] [Info:AgentSession] Handling Agent message for PieraC
Request: CallAction
CallDisposition:
这可能与简单的 AWK 或 SED 命令有关吗?
【问题讨论】:
-
将
print命令添加到第一个块。