This might be a dumb question but are ( ) supported by sed? I know \\( and \\) hold what is between them so you can access it by \1 or whatever number. I need a regex to look for parentheses and save what is between them.
sed 's/.*( .* \([[:alnum:]]+\) *).*/\1/' file
So I am looking for this:
possibly some stuff ( possibly some stuff [letters or numbers] possibly some stuff )
I need the outer ()s to be found, but you can’t \ them or else it thinks you want to save what is inside. Basically I only need to find the ()s so I can save some stuff inside them.
I am going to keep looking for how to do this but if any of you know how it would really help me out. I tried using "" and '' and unless I used incorrect syntax of some kind that doesn’t work. Thank you for any help you can provide.
Edit:Specific example: I want:
aname = (type name){ value, value }.
I want to know how to look for ()s and {} specifically in sed because they have other meanings. I assume \{ and \} is how you get {}s but I am not sure how to get ()s. Also I know the previous sed expression won’t find that, it was just a quick example I wrote down in order to show that I want to look for () in the regex. I am sorry if I have caused any confusion. Just so you guys can see this is my current regex:
start line aname = ( type name ) { values };
sed 's/^\([[:alnum:]]\)+ *={1} *[^()]({1} *[[:alnum:]]+ +\([[:alnum:]]\)+ *) *\{{1}\(.*\)\};/init_\2\( \&\1, \3)/' file
sed (and other utilities which use Posix Basic Regular Expressions) don’t give any special meaning to
(and)so you can just use them like any other character.\(and\)are the group delimiters.BRE’s really should die. Long, long ago, Posix defined “Extended” Regular Expressions (EREs) in which the grouping delimiters are
(and)and you use\(and\)to match parentheses, which is a lot more intuitive. There is a similar difference between BRE and ERE treatment of{and}(numerical repetition operators). EREs also include the|and+operators.grepandsedboth default to using BREs, but can be persuaded to use EREs with the correct command-line flag:grep -Eandsed -r. gawk uses EREs with some GNU extensions and some deviations from the standard, including the fact that you must specify –re-interval if you want to use{and}. [rant]It sometimes amazes me that we tolerate all this insanity. There should be just one regex standard, and all utilities should use it without exceptions.[/rant]If your regex with
(and)is not working as you expect, please provide specific examples of patterns which it fails on, and your expected outcome. My suspicion is that it has to do with the space characters in your pattern, if that is actually the pattern you’re using.