【问题标题】:list the uniq lines based on ":" delimiter [duplicate]列出基于“:”分隔符的 uniq 行 [重复]
【发布时间】:2015-05-14 16:54:39
【问题描述】:

我正在尝试编写一个脚本,该脚本将根据列/分隔符找到唯一的行(第一次出现)。在这种情况下,据我了解,分隔符是“:”。

例如:

May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780852: ndtpq.c(20544): this is the log  
May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780853: ndtpq.c(20544): this is another log  
May 14 00:00:02  SERVER1 ntp[1006]:  ntpd[Info]: 1430748798.780852: ndtpq.c(20544): this is another log  
May 14 00:00:03  SERVER1 ntp[1006]:  ntpd[Info]: 1430748799.780852: ndtpq.c(20544): this is the log  
May 14 00:00:04  SERVER1 ntp[1006]:  ntpd[Info]: 1430748800.780852: ndtpq.c(20544): this is the log  
May 14 00:00:04  SERVER1 ntp[1006]:  ntpd[Info]: 1430748800.790852: ndtpq.c(20544): this is the log  
May 14 00:00:05  SERVER1 ntp[1006]:  ntpd[Info]: 1430748801.790852: ndtpq.c(20544): thisis really different log  

想要的输出:

May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780852: ndtpq.c(20544): this is the log  
May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780853: ndtpq.c(20544): this is another log  
May 14 00:00:05  SERVER1 ntp[1006]:  ntpd[Info]: 1430748801.790852: ndtpq.c(20544): thisis really different log  

我可以使用以下命令找到 uniq 日志,但是使用这种方式会丢失时间戳。

cat fileName |awk -F: '{print $7}'

【问题讨论】:

  • 确定应该分组的条目的标准是什么?是消息的内容吗?如果是这样,如果稍后生成相同的消息怎么办?
  • 是消息的内容,以后看到可以忽略。只需要第一次出现。

标签: linux shell unix awk sed


【解决方案1】:

这可能会:

awk -F: '!seen[$NF]++' file
May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780852: ndtpq.c(20544): this is the log
May 14 00:00:01  SERVER1 ntp[1006]:  ntpd[Info]: 1430748797.780853: ndtpq.c(20544): this is another log
May 14 00:00:05  SERVER1 ntp[1006]:  ntpd[Info]: 1430748801.790852: ndtpq.c(20544): thisis really different log

它使用: 拆分文件,然后查看最后一个字段,并仅打印唯一的。

【讨论】:

  • 我得到的错误:看到[:找不到事件。
  • @user2809888 :您正在调用 shell 的历史记录替换。用单引号括住感叹号
【解决方案2】:

试试这个

Awk

 awk -F: '!x[$NF]++' infile

GNU 排序如果顺序无关紧要

 sort -u -t: -k7 infile

【讨论】:

  • 你的awk 和我已经发布的一样。
  • x[: 未找到事件。
  • @user2809888 :您正在调用 shell 的历史记录替换。用单引号括住感叹号
  • 谢谢,为我工作,我发现排序更甜更简单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2023-04-02
  • 2021-05-30
  • 2010-09-24
  • 1970-01-01
相关资源
最近更新 更多