user2677657
user2677657

Reputation: 11

Using sed , awk to extract lines of data between a pattern till the beginning of a line with a special character

i have the following 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>

i want to search for a particular line with orderid "example OrderId 100" and print both that line and everything below it till the next order line starting with doulbe hash(##) so if i search with orderid 100 i should get the following

## 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>

Upvotes: 1

Views: 241

Answers (2)

potong
potong

Reputation: 58488

This might work for you (GNU sed):

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

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85865

Your records are nicely separated so set the record separator to ## and search for the OrderId you want:

$ 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>

Upvotes: 2

Related Questions