Recursive Grep between two words and print on matched word

Hi All,
I've tried this a few different ways however cant seem to work it out.

Given this input file : "$newContents"
Calibre/Item:  Shotgun Shell Holder
Make:  Red Quoll
Model:  Black
Condition:  New

Calibre/Item:  Shotgun Shell Holder
Make:  Red Quoll
Model:  DCPU
Condition:  New

Calibre/Item:  Reloading
Make:  Remington
Model:  357 cases and dies
Condition:  Excellent
State: NSW

Calibre/Item:  30-06
Make:  Ruger
Model:  M77 Mk.II
Action:  Bolt repeater
Scope/Sights:  Zeiss 3-9x40
Condition:  Excellent

Calibre/Item:  222 Rem Mag
Make:  Sako
Model:  L461
Action:  Bolt repeater
Scope/Sights:  Sako rings
Condition:  Good
State: QLD

Calibre/Item:  9mm
Make:  STI
Model:  Custom 1911
Action:  Semi auto
Barrel Length:  152mm Holland Precision
Condition:  Excellent

Open in new window

Lets say I want to dispaly all the lines between "Calibre/Item" and "http" if  "9mm" is found.
I am using this, which works perfectly

awk '/Calibre\/Item:  9mm/,/http/'  $newContents >>  outputfile.txt

However, When I need to search for "Make" which is the second line in the block, I still need to display everything from "Calibre/Item" and "http". However I can't use awk (becuase I'm not searching for 9mm any more).
SO I've been doing this.

grep -i -B1 -A6 'STI'    $newContents >>  outputfile.txt

I'd like to have a generic grep/awk/sed whatever, where no matter which line in this block
I searched :

Calibre/Item:  9mm
Make:  STI
Model:  Custom 1911
Action:  Semi auto
Barrel Length:  152mm Holland Precision
Condition:  Excellent

It would always display the contents between "Calibre/Item" and "http"

I want to be able to match text on any line : "Custom1911", "$2800", "Semi Auto"

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this...
perl -ne 'BEGIN { $/ = "\n\n" } print if /regex/" inputfile > outputfile

Open in new window

Just replace /regex/ with what you want to search for (/Custom 1911/, /\$2800/, /Semi Auto/, etc).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samabdelhamidAuthor Commented:
Works a charm. Just had to replace the double quote with a single quote after '/regex/'

THank you

perl -ne 'BEGIN { $/ = "\n\n" } print if /regex/' inputfile > outputfile

Open in new window

for text in 'Custom1911' '$2800' 'Semi Auto' ; do perl -00 -ne 'BEGIN{$r=shift};print if /\Q$r\E/' "$text" $newContents  >>  outputfile.txt ; done
Sorry, I don't remember seeing an answer when I posted.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.