我正在尝试解析 apache 日志文件。基本上,我想在日志文件中 grep 查找 11:00,然后输出当时发生的每一行的最后六位数字。
我迷茫了。有人可以帮忙吗?
最佳答案
像这样的东西可以做到:
awk '/11:00/ {print substr($0,length($0) - 6)}' file
说明
/11:00/{}
匹配包含11:00
的行并执行{}
中的操作。print substr($0,length($0) - 6)
打印这些行的最后 6 个字符:从“字符串长度 - 6”开始,因为它打印从该点到结尾的所有内容。
测试
$ cat a
hello bye bye
11:00 hello bye bye basd
11:23 hello this is
another line
11:00 with some text
$ awk '/11:00/ {print substr($0,length($0) - 6)}' a
ye basd
me text
<小时/>
更新
根据您的评论:
Hey, I just checked and it works! Except, my mistake .. it's the last field that I need in the log, which actually varies in length, so I'm guessing that I need some way of 'cutting' everything after the last space! –
让我们尝试以下操作:
awk '/11:00/ {print $NF}' file
说明
/11:00/{}
匹配包含11:00
的行并执行{}
中的操作。print $NF
打印最后一个字段。
测试
$ cat a
hello bye bye
11:00 hello bye bye basd
11:23 hello this is
another line
11:00 with some text
$ awk '/11:00/ {print $NF}' a
basd
text
关于linux - 输出最后六个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115922/