This is an extension of my previous question. In that question, I needed to retrieve the text between parentheses where all the text was on a single line. Now I have this case:
(aop)
(abc
d)
This time, the open parenthesis can be on one line and the close parenthesis on another line, so:
(abc
d)
also counts as text between the delimiters ‘( )‘ and I need to print it as
abc
d
EDIT:
In response to possible confusions of my question, let me clarify a little. Basically, I need to print text between delimiters which could span multiple lines.
for example I have this text in my file:
randomtext(1234
567) randomtext
randomtext(abc)randomtext
Now I want Sed to pick out text between the delimiter “(” and “)”. So the output would be:
1234
567
abc
Notice that the left and right brackets are not on the same line but they still count as a delimiter for 1234 567, so I need to print that part of the text. (note, I only want the text between the first pair of delimiters).
Any help would be appreciated.
Ah! another tricky sed puzzle 🙂
I believe this code will work for your problem:
OUTPUT
For the provided input it produced:
Explanation:
-nsuppresses the regular sed output/(/,/)/is for range selection between(and):ais for marking a label a$!Nmeans append the next line of input into the current pattern space/)/!means do some actions if)is not matched in current pattern space/)/!${!ba}means go to labelaif)is not matched in current pattern spaces/.*(\([^)]*\)).*/\1/means replace content between(and)by just the content thus stripping out parenthesis\1is for back reference of group 1 i.e. text between\(and\)pis for printing the replaced content