checkmate
checkmate

Reputation: 267

Finding paragraphs with containing matches and extracting fields

I have a xml file and i need to get the 5th word of the 1st line if the word "bad" is available between start and end characters. How do I do this by using awk or sed in **Linux.

start check the label 12
d44 bad
d534 good
d443 bad
d432 bad
end

start check the label 15
d534 good
d532 good
end

start check the label 17
d52 bad
d422 good
end

need to do

if (between start and end "bad" available) then print 5th character

expected output

12
17

Upvotes: 1

Views: 73

Answers (3)

Kalanidhi
Kalanidhi

Reputation: 5092

You can also use sed command

sed -r ':loop;N;/(\n$|end$)/{s/([^0-9]+) ([0-9]+) .*bad.*/\2/gp;d;b};s/\n/ /g;t loop' file

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174756

Through awk,

$ awk -v RS="" '/start.*bad.*end/{print $5}' file
12
17

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85845

Simply with GNU awk:

$ awk '/bad/{print $5}' RS='end' file
12
17

Upvotes: 3

Related Questions