2

我需要在下面搜索“WAM”/var/log/messages并将完整的时间戳值导出到 Excel 工作表/文本文档

2013-07-09T02:22:18.535639Z user.info WebAppMgr WAM
2013-07-09T02:22:21.817372Z user.info sam SAM  ^Icom.palm.app.calculator
2013-07-09T02:22:21.818442Z user.info sam SAM  ^Icom.palm.app.settings
2013-07-09T02:22:22.746751Z user.info WebAppMgr WAM
2013-07-09T02:22:23.846636Z user.info sam SAM  ^Icom.palm.app.notes
2013-07-09T02:22:24.851727Z user.info sam SAM  ^Icom.palm.app.firstuse

对于终端输出,这将起作用

awk '/\ WAM/ {print $1"\t"}' /home/santosh/messages

我需要在文本文件/ Excel 表输出中

WAM 
2013-07-09T02:22:18.535639Z
2013-07-09T02:22:22.746751Z
4

4 回答 4

3

尝试这个:

awk 'BEGIN{print "WAM"}/\<WAM\>/{print $1}' /home/santosh/messages > text.file
于 2013-07-10T20:17:48.863 回答
2

试试这个单行:

awk 'BEGIN{v="WAM";print v}$NF==v&&$0=$1' yourfile

用你的例子:

kent$  echo "2013-07-09T02:22:18.535639Z user.info WebAppMgr WAM
2013-07-09T02:22:21.817372Z user.info sam SAM  ^Icom.palm.app.calculator
2013-07-09T02:22:21.818442Z user.info sam SAM  ^Icom.palm.app.settings
2013-07-09T02:22:22.746751Z user.info WebAppMgr WAM
2013-07-09T02:22:23.846636Z user.info sam SAM  ^Icom.palm.app.notes
2013-07-09T02:22:24.851727Z user.info sam SAM  ^Icom.palm.app.firstuse"|awk 'BEGIN{v="WAM";print v}$NF==v&&$0=$1'
WAM
2013-07-09T02:22:18.535639Z
2013-07-09T02:22:22.746751Z
于 2013-07-10T20:24:36.643 回答
1

尝试

awk '/\ WAM/ {print $1"\t"}' /home/santosh/messages > ~/testOutput

您始终可以从中导入 Excel 中由制表符分隔的文件。

于 2013-07-10T20:14:56.733 回答
1

重击:

grep WAM /var/log/messages | cut -d' ' -f1 > /home/santosh/messages

Python:

with open('/var/log/messages') as infile, open('/home/santosh/messages', 'w') as outfile:
    outfile.write("WAM\n")
    for line in (line for line in infile if "WAM" in line):
        outfile.write(line.partition(' ')[0]
        outfile.write('\n')
于 2013-07-10T20:20:07.530 回答