使用sed,awk在模式之间提取数据行,直到具有特殊字符的行的开头

时间:2013-08-13 07:52:52

标签: sed awk grep

我有以下xml

## 13 Aug 2013 14:53:44, 390 [INFO] OrderId 100 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>
## 13 Aug 2013 14:53:44, 390 [INFO] OrderId 105 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>
## 13 Aug 2013 14:55:45, 490 [INFO] OrderId 100 otherInfo  
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>
## 13 Aug 2013 14:53:44, 390 [INFO] OrderId 105 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>

我想用orderid“示例OrderId 100”搜索特定行并打印该行及其下面的所有内容,直到下一个以doulbe hash(##)开头的订单行 所以,如果我用orderid 100搜索,我应该得到以下

## 13 Aug 2013 14:53:44, 390 [INFO] OrderId 100 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>
## 13 Aug 2013 14:55:45, 490 [INFO] OrderId 100 otherInfo 
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>

2 个答案:

答案 0 :(得分:2)

您的记录很好地分开,因此请将记录分隔符设置为##并搜索您想要的OrderId

$ awk '/OrderId 100/{print RS $0}' RS='##' ORS=''  file
## 13 Aug 2013 14:53:44, 390 [INFO] OrderId 100 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>
## 13 Aug 2013 14:55:45, 490 [INFO] OrderId 100 otherInfo
<someXML>details
<info>details<info>
<info1>details<info1>
</someXML>

答案 1 :(得分:0)

这可能适合你(GNU sed):

sed '/OrderId 100/!d;:a;$!{n;/^##/{s/^/\n/;D};ba}' file