Remove bracket from a particular string

Viewed 133

If some text is like

cell (ABC)
(A1)
(A2)
function (A1.A2)

I want output as

cell ABC
A1
A2
function (A1.A2)

I want to remove bracket from each line of file except the present in function line. Using code

sed 's/[()]//g' file 

Removes bracket from each line. How can I modify the above code to get desired output.

2 Answers

You can add a jump out condition to your sed command:

sed '/^function /b;s/[()]//g' file

Or, condition the substitute on not matching a function:

sed '/^function /!s/[()]//g' file

Could you please try following. Written and tested with shown samples in GNU awk.

awk '!/function/{gsub(/[()]/,"")} 1' Input_file

Explanation: Adding detailed explanation for above.

awk '                ##Starting awk program from here.
!/function/{         ##Checking condition if line does not have function in it then do following.
  gsub(/[()]/,"")    ##Globally substituting ( OR ) with null in current line.
}
1                    ##1 will print current line.
' Input_file         ##Mentioning Input_file name here.
Related