Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp to delete text between parens

Status
Not open for further replies.

SirHammer1

Technical User
Sep 4, 2001
5
US
Hello Awk Masters!
I've been trying in vain for hours to develop a regexp for use in gsub or sub that will delete all text between parens on a given line, I'd like the parens to go also.

For example:
My knee hurts ( really bad )

Should come out
My knee hurts

In addition, there is another place in my file where I'd like to take the words in parens and put them into variables.

Can anyone please help?
Thanks for the assistance!
SirHammer1
 
I'm ashamed to say I don't know how to search for a ( in awk. I hope someone else responds to let us know. Here's a program which should do what you want including saving the text within parens into a variable.

{
s1 = $0
s2 = ""
ix1 = index(s1,"(")
if (ix1 > 0) {
ix2 = index(substr(s1,ix),")")
if (ix2 > 0) {
s2 = substr(s1,ix1,ix2)
s1 = substr(s1,1,ix1-1) substr(s1,ix1+ix2)
}
}
print s1
if (s2 != "") print s2
}

Hope this helps

CaKiwi
 
Ok, ok I figured out the regular expression for parentheses

{
gsub("[(].*[)]","")
print
}

Hope this helps.

CaKiwi
 
Thanks, worked like a champ! The other will come in handy as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top