【问题标题】:Using sed to cut up a log file使用 sed 剪切日志文件
【发布时间】:2020-05-25 03:58:32
【问题描述】:

我正在跟踪一个日志文件并使用 grep 来删除我想要信息的行,现在我想将其通过管道传送到 sed 中以修剪脂肪本身。

例如原始日志是:

Feb  9 17:48:21 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21
Feb  9 17:48:21 dnsmasq[884]: forwarded captive.g.aaplimg.com to 8.8.4.4
Feb  9 17:48:21 dnsmasq[884]: reply captive.g.aaplimg.com is 17.253.55.202
Feb  9 17:48:21 dnsmasq[884]: reply captive.g.aaplimg.com is 17.253.55.204

然后我使用 grep --line-buffered "query" 来获取查询行:

Feb  9 18:42:21 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21
Feb  9 18:42:40 dnsmasq[884]: query[A] sb.scorecardresearch.com from 192.168.178.21
Feb  9 18:42:51 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21
Feb  9 18:43:06 dnsmasq[884]: query[A] captive-cidr.origin-apple.com.akadns.net from 192.168.178.21
Feb  9 18:43:06 dnsmasq[884]: query[AAAA] captive-cidr.origin-apple.com.akadns.net from 192.168.178.21
Feb  9 18:43:21 dnsmasq[884]: query[A] time-macos.apple.com from 192.168.178.21

现在我有一个命令:

sudo tail -F /var/log/pihole.log  | grep --line-buffered "query" | sed -E 's/(\query).*(\from)/\1 \2/'

因为我想剪掉元素所以它去:

18:42 captive.g.aaplimg.com
18:42 sb.scorecardresearch.com
18:42 captive.g.aaplimg.com
18:43 captive-cidr.origin.apple.com 

等等。

我哪里出错了?

【问题讨论】:

  • 欢迎来到 Stack Overflow。您没有尝试从第三个字段(例如18:42)捕获时间。编写将执行您想要的操作的 sed 命令很简单,但也很乏味。你能写一个删除领先日期(Feb. 9 )吗?
  • 我对 sed、unix 和一般编码真的很陌生,老实说我真的不确定,我创建这个帐户是出于绝望,想看看要学什么哈哈哈,你能指出我的正确吗方向?
  • 即使我尝试通过以下方式缩短时间: sed -r 's/[0-9]{1,2}\:[0-9]{1,2}\1\ 2/' 我一无所有
  • 我不知道您使用的是什么风格的 sed,但这似乎不是一个格式正确的替换命令。试试sed 's/.......//' filename 看看会发生什么。你了解它的工作原理吗?
  • @KallumR,您能解释一下为什么输出只包含 那些 4 行吗?为什么它也不包含18:43 time-macos.apple.com

标签: sed grep


【解决方案1】:

以下命令从您描述为 @ 的输出的 6 行中提取您想要的信息(小时、分钟以及 queryfrom 之间的内容,正如您在最后 4 行代码块中描述的那样) 987654323@。

sed -E 's/.*([0-9]{2}:[0-9]{2}):.*query\[[^]]*\] (.*) from .*/\1 \2/'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2012-08-05
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2017-06-18
    相关资源
    最近更新 更多