0

我有一个日志文件,其中包含类似的条目

INFO 2013-08-16 13:46:48,660 Index=abc:12 insertTotal=11  ERROR: [doc=abc:d1c3f0]
INFO 2013-08-16 13:46:48,660 Index=abcd:12 insertTotal=11 ERROR: [doc=def:d1cwqw3f0]
INFO 2013-08-16 13:46:48,660 Index=def:134 insertTotal=11  
INFO 2013-08-16 13:46:48,660 Index=abkfe insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=lmkfe insertTotal=11
INFO 2013-08-16 13:46:48,660 Index=lmkfe insertTotal=11

我需要 grep [doc=]之间的部分,即 abc:d1c3f0 和 def:d1cwqw3f0 所以我想做类似 ^(abc|def)*]$

4

2 回答 2

4

or sed:

sed -n 's/.*\[doc=\(.*\)\].*/\1/p' filename

-n: don't print lines

.*\[doc= match anything that ends with [doc=

\(.*\) store as many characters as you can in a buffer while still finishing the match

\].* match a ] followed by as much as possible

\1 replace all that was matched with the contents of the \(.*\)

p print this line

于 2013-08-20T08:10:30.443 回答
1

grep救援:

$ grep -Po '(?<=\[doc=)[^\]]+' file
abc:d1c3f0
def:d1cwqw3f0

它获取从doc=(?<=\[doc=)部分)到]字符([^\]]+部分)之前的所有内容。

或与awk

$ awk -F"[][=]" '{print $5}' file
abc:d1c3f0
def:d1cwqw3f0

-F"[][=]"定义不同的可能字段分隔符[]=. 然后,它打印第 5 个“片”。

于 2013-08-20T08:08:36.600 回答