我想打印前第四行(Calling_party_IP)和第二行(被叫号码),因为每次有 INVITE 消息时(CSeq:1 INVIte)都会内联出现
示例输入:
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP calling_party_IP:58929;branch=z9hG4bK93464974;received=calling_party_IP;rport=34653
From: <sip:98745@ip>;tag=123456
To: <sip:called number@ip>;tag=as4463463edc
Call-ID: some string
CSeq: 1 INVITE
Server:
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, PRACK, MESSAGE
Supported: replaces, timer
Content-Length: 0000
样本输出:
calling_party_IP
called number
答案1
根据我对问题含义的猜测,我有这个。 (编辑:问题已得到改进,我的猜测似乎是正确的。)
基本思想是将每一行存储到一个 4 长数组中,当匹配时打印出之前的第 2 行和第 4 行。所以输入
ant
bee
cat
dog
eel
fun
寻找fun
将打印出bee
和dog
。
awk '/fun/{ print saved[(NR-4)%4],saved[(NR-2)%4]} {saved[NR%4]=$0}' file
这允许以图案结束的 5 条线的重叠块。