Question

I've lines of HTML that contains newlines and stuffs, I want to replace a sentence like this :

<li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;
">You &amp; Your Service Dog</a></li>

notice the newline before ">You

I have the first part, of finding the snippet

pcregrep  -r -M -l '<li class="menu-581">(.|\n)*?</li>' *

Now , I want to feed it into Perl to replace the same regex with new text

I'm trying this perl snippet but it doesnt work (i'm saving it in a file called test.txt):

ls test.* | xargs perl -pe 's/<li class="menu-581">(.|\n)*<\/li>/new/' -pi

Any clue?

Was it helpful?

Solution

Edit: This one I've actually tested, so it is working as expected:

 ls test.* | xargs perl -pe 'BEGIN{undef $/;};s/(<li class="menu-581">)(.*)(<\/li>)/$1$3/gs'

OTHER TIPS

cat -e test.html
<p>$
<li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;$
">You &amp; Your Service Dog</a></li>$
</p>$

Then

perl -0777 -pe 's{\Q<li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;
">You &amp; Your Service Dog</a></li>}{}g' test.html
<p>

</p>

Add the -i flag to edit in-place:

perl -0777 -i -pe '...' test*
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top