linux - 输出最后六个字符串

标签 linux apache grep

我正在尝试解析 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/

相关文章:

linux - 如何在 linux 中创建自签名 SSL 证书以及在获取 ssl 后对 apache ssl conf 文件进行哪些修改?

c - 我如何知道 C 程序的可执行文件是在前台还是后台运行?

linux - bash 脚本上的 "Cache"凭据

java - apache jmeter 测试脚本记录器

apache - 在 Tomcat 前面有一个 apache 网络服务器的原因是什么?

regex - zshell grep 负面回顾

linux - 如何只打印包含特定字符串的文件的文件名部分?

linux - 为什么运行一个空程序需要这么多指令?

apache - 如何使用 mod_rewrite 在内部重写/到/homepage

linux - 从文件中提取一个单词并将文件另存为该单词