Shell脚本程序提取消息中的部分时间戳.log文件



我在 v 中有如下数据ar/log/messages.log现在我需要搜索 WAM 数据行并仅提取部分时间戳

例如

2013-07-09T02:22:28.535639Z [24] user.info WebAppMgr WAM APPLAUNCH_INITIATED

上行包含WAM,我只需要在消息中22:28.535639Z数据.log

2013-07-09T02:22:28.535639Z [24] user.info WebAppMgr WAM APPLAUNCH_INITIATED 
2013-07-09T02:22:21.817372Z [17] user.info sam SAM  ^Icom.palm.app.calculator
2013-07-09T02:22:21.818442Z [17] user.info sam SAM  ^Icom.palm.app.settings
2013-07-09T02:24:04.738067Z [120] user.info WebAppMgr WAM APPLAUNCH_INITIATED 
2013-07-09T02:22:21.846636Z [17] user.info sam SAM  ^Icom.palm.app.notes
2013-07-09T02:22:21.851727Z [17] user.info sam SAM  ^Icom.palm.app.firstuse
2013-07-09T02:22:21.854172Z [17] user.info sam SAM  ^Icom.palm.app.isis2
2013-07-09T02:22:21.863786Z [17] user.info sam SAM  ^Icom.palm.sysapp.voicedial
2013-07-09T02:24:04.746751Z [120] user.info WebAppMgr WAM APP CREATED WINDOW

我能够提取2013-07-09T02:22:28.535639Z.我需要知道如何提取22:28.535639Z

#! /bin/sh
awk '/ WAM/ {print $1"t"}' /home/santosh/messages

我得到的输出是这样的

2013-07-09T02:22:28.535639Z
2013-07-09T02:24:04.738067Z
2013-07-09T02:24:04.746751Z

但我只需要下面的数据

22:28.535639Z
24:04.738067Z
24:04.746751Z

您可以在当前的 awk 调用中执行此操作:

awk '/<WAM>/ {split($1, a, ":"); print a[2] ":" a[3]}' file

<>是词边界断言。

with open('path/to/logfile') as logfile:
    for line in logfile:
        if "WAM" in line:
            timestamp = line.partition(" ")[0].partition(":")[2]
            print timestamp

在您的示例上运行上面的代码,我得到这个作为输出:

22:28.535639Z
24:04.738067Z
24:04.746751Z

使用日期时间模块:

>>> from datetime import datetime
>>> strs = "2013-07-09T02:22:28.535639Z"
>>> d = datetime.strptime(strs,'%Y-%m-%dT%H:%M:%S.%fZ')
>>> d.strftime('%M:%S.%fZ')
'24:04.746751Z'

法典:

with open('/home/santosh/messages') as f:
    for line in f:
        if 'WAM' in line:
            d = datetime.strptime(line.split()[0],'%Y-%m-%dT%H:%M:%S.%fZ')
            print d.strftime('%M:%S.%fZ')
...             
22:28.535639Z
24:04.738067Z
24:04.746751Z

根据您使用的标签和您给出的示例,除了基于 Python 的解决方案之外,您似乎对 shell 解决方案持开放态度。由于多样性是生活的调味品,因此请使用sed

$ sed -n  '/WAM/{s/.*T[0-9]*:([0-9]*:[0-9]*.[0-9]*Z).*/1/g;p}' /home/santosh/messages 
22:28.535639Z
24:04.738067Z
24:04.746751Z

对于包含"WAM"的任何行,找到与模式"[anything]Tdigits:(digits:digits.digitsZ([anything]"匹配的文本,然后将该行替换为括号中的匹配文本部分("digits:digits.digtsZ"(,然后打印出来。切换到sed -n只是意味着不要打印任何内容,除非您告诉它(即使用 p 命令(。

另一种awk方式:

awk -F':| ' '/<WAM>/{print $2":"$3}' /home/santosh/messages

使用 regex

蟒:

import re
with open('/home/santosh/messages') as f:
    for line in f:
        m = re.search(r'^.*?:(S+).*?WAM',line)
        if m: print m.group(1)

Perl:

while ($line = <STDIN>){
    if ($line =~ m/^.*?:(S+).*?WAM/){
        print "$1n";
        }
}

输出:

$ perl so.pl < abc
22:28.535639Z
24:04.738067Z
24:04.746751Z
cat test.txt | cut -d " " -f 1 | cut -d "T" -f 2 | cut -d ":" -f 2-3

在文件中添加了您的数据...我"cut"命令可以做到这一点...

一个纯粹的 bash 解决方案:

while read a x x x b x; do
  [ "$b" == WAM ] && echo ${a#*:}
done </var/log/messages.log

输出:

22:28.535639Z
24:04.738067Z
24:04.746751Z

最新更新