2

I have a requirement like i have to remove the strings or numbers between two Parenthesis in a file. I used sed command but it is working on a single line. My opening Parenthesis is in one line and the closing parenthesis is in other line. How can i do it?

I tried this command using sed:

sed -e 's/([^()]*)//g'

but this works only when My opening and closing parenthesis are on the same line.

For Example:-

Input file:

select a
,b
,c
FROM ABCD
(select e
,f
,g
,h FROM XYZ)

Output should be:

select a
,b
,c
FROM ABCD

  • What have you tried - what modifications of the original sed command did you attempt? What were the results? – John Apr 14 '15 at 12:29
  • 1
    For new GNU sed use -z option – Costas Apr 14 '15 at 12:33
  • I tried using sed command sed -e 's/([^()]*)//g' <filename>> this worked for single line ... it didn't worked if parenthesis are on different line – Priyanka Apr 14 '15 at 12:34
4

Use the following simple perl script to remove every pair of parentheses and their content, even across multiple lines:

#!/usr/bin/perl
undef $/;
$text = <>;
#Flags: g=match repeatedly; s=dot matches newline
$text =~ s/\(.*?\)//gs;
print $text;

If you want to fit it into the commandline, here's the one-liner version:

perl -p0777e 's/\(.*?\)//gs' [filename]

Note that it's shorter and simpler than the perl solutions. -0777 disables the line separator (see the -0 flag under man perlrun), causing the whole file to be processed in one step. Good old perl... It's also (unusually for perl :-)) more readable than messing with sed's pattern space.

  • thanks ... But I need to implement this using Unix only, not perl – Priyanka Apr 15 '15 at 7:51
  • 2
    What do you mean you "have to"? Perl is as much part of unix as sed. If you want something that fits on the commandline, I've added a version. – alexis Apr 15 '15 at 8:34
3

To remain \new line (if parenthesis in two consequent lines)

sed -e 's/\(^[^)]*) *\)\|([^)]*\() *\|$\)//g' filename

The script consists three patterns:

  • ^[^)]*) * from line begining any symbol(s) exept ) till ) with space(s) after;
  • ([^)]* from ( any symbol(s) exept )
    • till ) with space(s) after; or
    • till $ (end of line)

For multiline cases:

sed ':1;s/([^)]*)//g;/(/{N;b1};' filename
  • But this command is not deleting the content between the opening and closing parenthesis instead it is just removing the lines containing opening and closing parenthesis – Priyanka Apr 14 '15 at 12:51
  • @Priyanka Add for multilines – Costas Apr 14 '15 at 13:16
  • thank u it worked. I am new to unix scripting. need to learn alot – Priyanka Apr 14 '15 at 13:18
  • @Priyanka For future: it is right way to receive correct answer - post example input and desired output – Costas Apr 14 '15 at 13:31
2

Using Gnu Sed.

-z option (Null separated records), sed slurps all the input as a single record. Please try:

sed -ze 's/([^()]*)//g'
1

For multi-line matching with sed, it's often easiest to read the whole file, and do your search/replace on the entire contents:

sed -n '            # disable auto-printing
  1h                # first line, move to hold space
  1!H               # not the first line, append to hold space
  ${                # at the end of file
    x               # move hold space to pattern space
    s/([^)]*)//gp   # perform search/replace and print
  }
' file

There are simpler(*) options:

awk -v RS="" '{gsub(/\([^)]+\)/,"")} 1' file
perl -0777 -pe 's/\(.*?\)//sg' file

(*) "simpler" may be in the eye of the beholder ...

  • Just a add On question to this... will it work if i have nested Parenthesis Staements – Priyanka Apr 14 '15 at 13:55
  • 4
    Nope. That kind of structure is beyond what regular expressions can handle. Once you get "nested", your language is no longer regular. – glenn jackman Apr 14 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.