Can I use sed if I need to extract a pattern enclosed by a specific pattern, if it exists in a line?
Suppose I have a file with the following lines :
There are many who dare not kill themselves for [/fear/] of what the neighbors will say.
Advice is what we ask for when we already know the
/*answer*/but wish we didn’t.
In both the cases I have to scan the line for the first occurring pattern i.e ‘ [/ ‘ or ‘/* ‘ in their respective cases and store the following pattern till then exit pattern i.e ‘ /] ‘or ‘ */ ‘ respectively .
In short , I need fear and answer .If possible , Can it be extended for multiple lines ;in the sense ,if the exit pattern occurs in a line different than the same .
Any kind of help in the form of suggestions or algorithms are welcome. Thanks in advance for the replies
As a one-liner:
The inner while loop will iterate between all matches with the
/gmodifier. The backreference\1will make sure we only match identical open/close tags.If you need to match blocks that extend over multiple lines, you need to slurp the input:
One-liner:
The
-0777switch and$/ = undefwill cause file slurping, meaning all of the file is read into a scalar. I also added the/smodifier to allow the wildcard.to match newlines.Explanation for the regex:
m#/(\*?)(.*?)\1/#sgThe “magic” here is that the backreference requires a star
*only when one is found before it.