Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 提取message.log文件中部分时间戳的Shell脚本程序_Linux_Bash_Shell - Fatal编程技术网

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

Linux 提取message.log文件中部分时间戳的Shell脚本程序,linux,bash,shell,Linux,Bash,Shell,我在var/log/messages.log中有如下数据,现在我需要搜索WAM数据行并只提取部分时间戳 比如说 2013-07-09T02:22:28.535639Z [24] user.info WebAppMgr WAM APPLAUNCH_INITIATED 上面的行包含WAM,我只需要messages.log中的22:28.535639Z数据 2013-07-09T02:22:28.535639Z [24] user.info WebAppMgr WAM APPLAUNCH_INITI

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

比如说

2013-07-09T02:22:28.535639Z [24] user.info WebAppMgr WAM APPLAUNCH_INITIATED
上面的行包含WAM,我只需要messages.log中的22:28.535639Z数据

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

我得到的输出像

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
在您的示例上运行上述代码,我得到以下输出:

22:28.535639Z
24:04.738067Z
24:04.746751Z
使用模块:

代码:


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


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

您可以在当前的awk呼叫中执行此操作:

awk '/\<WAM\>/ {split($1, a, ":"); print a[2] ":" a[3]}' file
\是单词边界断言。

使用正则表达式:

Python:

Perl:

输出:

awk的另一种方式:

已将数据添加到文件中。。。I cut命令可以完成此操作…

纯解决方案:

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

使用打开的“var/log/messages”作为日志文件:但抛出类似msg2.py:1:msg2.py:Syntax error:unexpected的错误我用python 2.7编写了我的代码。请确保您使用的是相同的版本brrr。。。使用了四个外部实用程序,cat是绝对无用的!
>>> 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
$ 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
awk '/\<WAM\>/ {split($1, a, ":"); print a[2] ":" a[3]}' file
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)
while ($line = <STDIN>){
    if ($line =~ m/^.*?:(\S+).*?WAM/){
        print "$1\n";
        }
}
$ perl so.pl < abc
22:28.535639Z
24:04.738067Z
24:04.746751Z
awk -F':| ' '/\<WAM\>/{print $2":"$3}' /home/santosh/messages
cat test.txt | cut -d " " -f 1 | cut -d "T" -f 2 | cut -d ":" -f 2-3
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