接下来的情况。我得到了日志文件,其中日志用减号字符分隔,如:
Timestamp1
---
Log: 1
Address: http://addr1.com
Payload: <soap:Envelope>
<soap:Body>
<context 1-1>
<context 1-2>
<context 1-3>
</soap:Body>
<soap:Envelope>
---;
Timestamp2
---
Log: 2
Address: http://addr2.com
Payload: <soap:Envelope>
<soap:Body>
<context 2-1>
</soap:Body>
<soap:Envelope>
---;
Timestamp3
---
Log: 3
Address: http://addr3.com
Payload: <soap:Envelope>
<soap:Body>
<context 3-1>
<context 3-2>
</soap:Body>
<soap:Envelope>
---;
...
我需要获取一些关键字找到的完整日志信息,例如如果关键字是&#34;上下文2-1&#34;应打印下一个字符串:
---
Log: 2
Address: http://addr2.com
Payload: <soap:Envelope>
<soap:Body>
<context 2-1>
</soap:Body>
<soap:Envelope>
---;
那么我如何使用&#34; greedy&#34;进行此模式搜索?切割周围的分界线?
答案 0 :(得分:1)
使用sed:
sed -n '/^---/ {:a;N;/---;/!ba;/context 2-1/p}' file
<强>解释强>
/^---/
开头的行---
找到了a
:标签为来循环N
:在模式空间中添加下一行/---;/!
:如果找不到---;
...... ba
循环到a
标签/context 2-1/p
:当循环终止时,如果找到context 2-1
,则打印之前添加到模式空间的所有行答案 1 :(得分:0)
将此作为指导原则:How to select lines between two patterns?
$0=="---" { # at the fron marker
flag=1 # flag up
buf="" # clear the buf
}
$0=="---;" { flag=0 } # at the end marker, flag down
{
buf=buf $0 (flag?RS:"") # gather buffer, add RS before the end marker
if($0 ~ "^Payload2") # if keyword found
output=1 # output flag up
}
flag==0 && output==1 { # after end marker when putput flag up
print buf # output buf
output=0 # output flag down
}
运行它:
$ awk -f script.awk logfile
---
Log2
Address2 ...
Payload2 ...
---;
答案 2 :(得分:0)
awk -vRS="Timestamp[0-9]+" -v k="context 2-1" '$0~k' file2
这使用Timestamp[0-9]+
作为换行符。 k
是您想要的关键字。如果$0
与关键字匹配,则打印$0
。