dimanche 8 mars 2015

Searching file for string surrounded by two other known string?


Question title may be kinda vague, but heres what im attempting to do.


I have a html file locally on my server which is changing fairly regularly. It has multiple occurrences of:



<p>Jumble of random text</p>
<p>More text</p>


And so on...


I want to be able to sort through all the different jumble of random text's till it finds the one I am looking for, for example 'random', which could be anywhere in the file. Then I want a variable assigned to



Jumble of random text


Everything between the:



<p> and </p>


So far I have this:



echo grep -i 'random' search.html | grep -o -P '(?<=<p>).*(?=</p>)' search.html


Which works to an extent, however it is finding the first occurrence and the last occurrence of the paragraph tags, therefore giving me a huge output of meaningless data, ie in this case:



Jumble of random text
More text


How can i get it to just return:



Jumble of random text


Getting very confused, very fast.



Aucun commentaire:

Enregistrer un commentaire