2017-08-04 141 views
1
sed -er '.*(textsringhere.+?(?=))(?:.*)((?>\d{4})-(?>\d{2})-(?>\d{2}) (?>\d{2}):(?>\d{2}):(?>\d{2})).* (ERROR.*)' errors.txt 

在我的errors.txt文件上運行上述命令只會生成該文件中的所有匹配行。我曾經假定在我的正則表達式的開頭添加.*會迫使sed用整個匹配替換整條線?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` 
+0

FWIW我也不能得到'grep的-o'工作 – Tony

+0

也嘗試添加了替代PARAM''S * ... /)' – Tony

+0

嘗試(?> \ d {4}) - (?> \ d {2}) - (?> \ d { (?> \ d {2}):(?> \ d {2}))。*(ERROR。*)。*/\ 1 /'errors.txt ' – Tony

回答

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