Sed和regex打印只匹配

时间:2017-08-04 17:04:56

标签: regex sed

sed -er '.*(textsringhere.+?(?= ))(?:.*)((?>\d{4})-(?>\d{2})-(?>\d{2}) (?>\d{2}):(?>\d{2}):(?>\d{2})).* (ERROR.*)' errors.txt

在我的errors.txt文件上运行上述命令只会生成该文件中的所有匹配行。我曾假设在我的正则表达式的开头添加.*会强制sed用匹配替换整行?

示例

输入:

Aug  2 16:36:37 App.Dev.thing1 839854b7-749-4f12-89e0-3ad002ab5ffe[[APP/PROC/WEB/0]] 2017-08-02 16:36:37 [main] ERROR o.s.boot.SpringApplication - Application startup failed
Aug  2 18:04:46 App.Dev.thing2 eaedf253-df57-4c12-ade6-ea73274dbbc4[[APP/PROC/WEB/0]] 2017-08-02 18:04:46 [main] ERROR o.s.boot.SpringApplication - Application startup failed
Aug  3 01:45:55 App.Dev.thing2 eaedf253-df57-4c12-ade6-ea73274dbbc4[[APP/PROC/WEB/0]] 2017-08-03 01:45:55 [http-nio-8080-exec-1] ERROR c.c.v.b.m.c.i.thing2 - Error Processing Batch, this batch will be consumed individually`

预期产出:

App.Dev.thing1 ERROR o.s.boot.SpringApplication - Application startup failed
App.Dev.thing2 ERROR o.s.boot.SpringApplication - Application startup failed
App.Dev.thing2 ERROR c.c.v.b.m.c.i.thing2 - Error Processing Batch, this batch will be consumed individually`

1 个答案:

答案 0 :(得分:0)

简单 sed 方法:

sed -E 's/.*(App\.\S+).+(ERROR .*)$/\1 \2/' errors.txt

输出:

App.Dev.thing1 ERROR o.s.boot.SpringApplication - Application startup failed
App.Dev.thing2 ERROR o.s.boot.SpringApplication - Application startup failed
App.Dev.thing2 ERROR c.c.v.b.m.c.i.thing2 - Error Processing Batch, this batch will be consumed individually
相关问题