VIKAS CHOUDHARY
VIKAS CHOUDHARY

Reputation: 55

Remove bracket from a particular string

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.

Upvotes: 4

Views: 323

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133508

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.

Upvotes: 6

perreal
perreal

Reputation: 97948

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

Upvotes: 7

Related Questions