使用正则表达式在日志中的时间戳之间添加换行符

我有一些日志数据:

2017-12-03 01:35:58 [Notice] syslog: local  IP address 
2017-12-03 01:35:58 [Notice] syslog: remote IP address 
2017-12-03 01:35:58 [Notice] syslog: primary   DNS address 
2017-12-03 01:35:58 [Notice] syslog: secondary DNS address 
2017-12-03 01:35:59 [Warning] kernel: Link State: PVC_8_0 logistic interface up.
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: started, version 2.52 cachesize 150
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:35:59 [Warning] dnsmasq[10463]: ignoring nameserver 127.0.0.1 - local interface
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.66#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.65#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: read /etc/hosts - 6 addresses
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: started, version 2.52 cachesize 150
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: using nameserver 87.216.1.66#53(via ppp80)
2017-12-03 01:36:01 [Warning] kernel: ^M
2017-12-03 01:36:01 [Warning] kernel: Send DNS Query : domain=ntp2.jazztel.com qType=A dnsServer=87.216.1.65
2017-12-03 01:36:01 [Warning] kernel: domain: ntp2.jazztel.com , IP: 87.216.1.241
2017-12-03 01:36:01 [Warning] kernel: sntp server=ntp2.jazztel.com: 0x5 ntpServerIP=87.216.1.241

我想在每次时间戳更改时添加一个换行符,因此它看起来像这样:

2017-12-03 01:35:58 [Notice] syslog: local  IP address 
2017-12-03 01:35:58 [Notice] syslog: remote IP address 
2017-12-03 01:35:58 [Notice] syslog: primary   DNS address 
2017-12-03 01:35:58 [Notice] syslog: secondary DNS address 

2017-12-03 01:35:59 [Warning] kernel: Link State: PVC_8_0 logistic interface up.
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: started, version 2.52 cachesize 150
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:35:59 [Warning] dnsmasq[10463]: ignoring nameserver 127.0.0.1 - local interface
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.66#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.65#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: read /etc/hosts - 6 addresses

2017-12-03 01:36:00 [Informational] dnsmasq[10532]: started, version 2.52 cachesize 150
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: using nameserver 87.216.1.66#53(via ppp80)

2017-12-03 01:36:01 [Warning] kernel: ^M
2017-12-03 01:36:01 [Warning] kernel: Send DNS Query : domain=ntp2.jazztel.com qType=A dnsServer=87.216.1.65
2017-12-03 01:36:01 [Warning] kernel: domain: ntp2.jazztel.com , IP: 87.216.1.241
2017-12-03 01:36:01 [Warning] kernel: sntp server=ntp2.jazztel.com: 0x5 ntpServerIP=87.216.1.241

这适用于https://regexr.com

s/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ).*\n(?!\1)/$0\n/g

但是,当我在Terminal(OSX)中尝试它时,它什么也没做:

curl -s http://192.168.1.1/cgi-bin/status_log2.cgi | grep 2017 | tail -n 30 | perl -pe 's/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ).*\n(?!\1)/$0\n/g'

我也尝试过gsedsed但无济于事。

(如果有办法完全删除所有多余的时间戳,请加分!)

约翰1024

在每个新时间之前设置换行符:

awk '!a[$1,$2]++ && NR>1{print ""} 1'

工作原理:在awk中,$1并且$2是第一场,在这种情况下,日期和时间。a[$1,$2]是一个关联数组,用于计算我们看到这两个字段的次数。如果之前已经看到了该日期和时间,!a[$1,$2]并且不在第一行上NR>1,那么我们将打印空白行以进行分隔print ""最终1只是打印当前行的简写。

例子

将您的样本登录到文件中logfile

$ awk '!a[$1,$2]++ && NR>1{print ""} 1' logfile
2017-12-03 01:35:58 [Notice] syslog: local  IP address 
2017-12-03 01:35:58 [Notice] syslog: remote IP address 
2017-12-03 01:35:58 [Notice] syslog: primary   DNS address 
2017-12-03 01:35:58 [Notice] syslog: secondary DNS address 

2017-12-03 01:35:59 [Warning] kernel: Link State: PVC_8_0 logistic interface up.
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: started, version 2.52 cachesize 150
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:35:59 [Warning] dnsmasq[10463]: ignoring nameserver 127.0.0.1 - local interface
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.66#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: using nameserver 87.216.1.65#53(via ppp80)
2017-12-03 01:35:59 [Informational] dnsmasq[10463]: read /etc/hosts - 6 addresses

2017-12-03 01:36:00 [Informational] dnsmasq[10532]: started, version 2.52 cachesize 150
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
2017-12-03 01:36:00 [Informational] dnsmasq[10532]: using nameserver 87.216.1.66#53(via ppp80)

2017-12-03 01:36:01 [Warning] kernel: ^M
2017-12-03 01:36:01 [Warning] kernel: Send DNS Query : domain=ntp2.jazztel.com qType=A dnsServer=87.216.1.65
2017-12-03 01:36:01 [Warning] kernel: domain: ntp2.jazztel.com , IP: 87.216.1.241
2017-12-03 01:36:01 [Warning] kernel: sntp server=ntp2.jazztel.com: 0x5 ntpServerIP=87.216.1.241

删除重复的时间戳

$ awk '{if(a[$1,$2]++){gsub(/./," ",$1); gsub(/./," ",$2)} else if (NR>1) print""} 1' logfile
2017-12-03 01:35:58 [Notice] syslog: local  IP address 
                    [Notice] syslog: remote IP address
                    [Notice] syslog: primary DNS address
                    [Notice] syslog: secondary DNS address

2017-12-03 01:35:59 [Warning] kernel: Link State: PVC_8_0 logistic interface up.
                    [Informational] dnsmasq[10463]: started, version 2.52 cachesize 150
                    [Informational] dnsmasq[10463]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
                    [Warning] dnsmasq[10463]: ignoring nameserver 127.0.0.1 - local interface
                    [Informational] dnsmasq[10463]: using nameserver 87.216.1.66#53(via ppp80)
                    [Informational] dnsmasq[10463]: using nameserver 87.216.1.65#53(via ppp80)
                    [Informational] dnsmasq[10463]: read /etc/hosts - 6 addresses

2017-12-03 01:36:00 [Informational] dnsmasq[10532]: started, version 2.52 cachesize 150
                    [Informational] dnsmasq[10532]: compile time options: no-IPv6 GNU-getopt no-RTC no-DBus no-I18N no-DHCP no-TFTP
                    [Informational] dnsmasq[10532]: using nameserver 87.216.1.66#53(via ppp80)

2017-12-03 01:36:01 [Warning] kernel: ^M
                    [Warning] kernel: Send DNS Query : domain=ntp2.jazztel.com qType=A dnsServer=87.216.1.65
                    [Warning] kernel: domain: ntp2.jazztel.com , IP: 87.216.1.241
                    [Warning] kernel: sntp server=ntp2.jazztel.com: 0x5 ntpServerIP=87.216.1.241

在这种情况下,如果我们之前看到过日期$1和时间$2,那么我们将其内容替换为空格gsub(/./," ",$1); gsub(/./," ",$2)如果不是,并且我们不在第一行,那么我们将打印空白行以进行分隔。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用正则表达式从CSV删除行中的换行符

来自分类Dev

正则表达式匹配星号与换行符之间的字符串

来自分类Dev

正则表达式换行符

来自分类Dev

如何在文本文件中的正则表达式之前和之后添加换行符?

来自分类Dev

正则表达式与换行符不匹配

来自分类Dev

正则表达式替换Windows换行符

来自分类Dev

使用正则表达式从包含换行符的文本中解析字符串

来自分类Dev

正则表达式在Notepad ++中替换管道之间的换行符

来自分类Dev

正则表达式使用换行符拆分字符串(除非它在双引号之间)

来自分类Dev

Perl6:使用正则表达式捕获字符串中的Windows换行符

来自分类Dev

正则表达式换行符遇到StackOverflowError

来自分类Dev

正则表达式以匹配换行符和括号之间的制表符

来自分类Dev

用于换行符解析的正则表达式

来自分类Dev

使用正则表达式删除两个标记之间的任意数量的换行符

来自分类Dev

使用正则表达式提取字符串和换行符(/ n)之间的文本

来自分类Dev

正则表达式,用于在逗号之间用换行符配对

来自分类Dev

正则表达式包括换行符

来自分类Dev

使用PowerShell将正则表达式中的换行符匹配

来自分类Dev

如何在文本文件中的正则表达式之前和之后添加换行符?

来自分类Dev

正则表达式匹配之前删除换行符\换行符

来自分类Dev

正则表达式替换Windows换行符

来自分类Dev

正则表达式:匹配字符或换行符之间的所有内容

来自分类Dev

正则表达式删除换行符

来自分类Dev

使用正则表达式的表单验证脚本可在换行符之间检查多个关键字

来自分类Dev

正则表达式在文本的每个点处添加换行符(定义的缩写除外)

来自分类Dev

正则表达式在每个括号后添加新的换行符

来自分类Dev

正则表达式包含换行符

来自分类Dev

如何使用 gedit 中的正则表达式函数在文件中查找替换换行符和逗号?

来自分类Dev

如何在python 2.7中使用正则表达式在列表中插入换行符?

Related 相关文章

  1. 1

    使用正则表达式从CSV删除行中的换行符

  2. 2

    正则表达式匹配星号与换行符之间的字符串

  3. 3

    正则表达式换行符

  4. 4

    如何在文本文件中的正则表达式之前和之后添加换行符?

  5. 5

    正则表达式与换行符不匹配

  6. 6

    正则表达式替换Windows换行符

  7. 7

    使用正则表达式从包含换行符的文本中解析字符串

  8. 8

    正则表达式在Notepad ++中替换管道之间的换行符

  9. 9

    正则表达式使用换行符拆分字符串(除非它在双引号之间)

  10. 10

    Perl6:使用正则表达式捕获字符串中的Windows换行符

  11. 11

    正则表达式换行符遇到StackOverflowError

  12. 12

    正则表达式以匹配换行符和括号之间的制表符

  13. 13

    用于换行符解析的正则表达式

  14. 14

    使用正则表达式删除两个标记之间的任意数量的换行符

  15. 15

    使用正则表达式提取字符串和换行符(/ n)之间的文本

  16. 16

    正则表达式,用于在逗号之间用换行符配对

  17. 17

    正则表达式包括换行符

  18. 18

    使用PowerShell将正则表达式中的换行符匹配

  19. 19

    如何在文本文件中的正则表达式之前和之后添加换行符?

  20. 20

    正则表达式匹配之前删除换行符\换行符

  21. 21

    正则表达式替换Windows换行符

  22. 22

    正则表达式:匹配字符或换行符之间的所有内容

  23. 23

    正则表达式删除换行符

  24. 24

    使用正则表达式的表单验证脚本可在换行符之间检查多个关键字

  25. 25

    正则表达式在文本的每个点处添加换行符(定义的缩写除外)

  26. 26

    正则表达式在每个括号后添加新的换行符

  27. 27

    正则表达式包含换行符

  28. 28

    如何使用 gedit 中的正则表达式函数在文件中查找替换换行符和逗号?

  29. 29

    如何在python 2.7中使用正则表达式在列表中插入换行符?

热门标签

归档