Perl/Sed: Delete a function that extends to multiple lines

I want to delete every call to a function XYZ. The problem is that this function might extend to multiple lines. E.g.

XYZ( "line 1",
     "line 2",
     "line 3" );

To simplify things, I have the assumption that ); is only present at the end of the call.

I have tried sed 's/XYZ(.*);//' without success, though in sed the . character matches any char including newline. I am probably missing something with the new lines in sed.

Thank you

Upvotes: 0

Views: 125

Answers (2)

JJoao
JJoao

Reputation: 5347

perl -p0e 's!\bXYZ\(.*?\);!!gs' input

Testing:

XYZ( "line 1",
     "line 2",
     "line 3" );

hello world

XYZ( "line 1",
     "line 2",
     "line 3" );
   XYZ(aaaa); XYZ(aaaa);  aaa(aaaa);

abcXYZ( aaaa );
   XYZ(aaaa);
   XYZabc(aa);

output is:

hello world


     aaa(aaaa);

abcXYZ( aaaa );

   XYZabc(aa);

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can use address range as

sed '/^XYZ/, /);$/ d' inputFile
  • /^XYZ/ start address

  • /);$/ end address

  • d delete command. Deltes line from pattern space in the address range provided

Test

$ cat input
XYZ( "line 1",
     "line 2",
     "line 3" );

hello world

XYZ( "line 1",
     "line 2",
     "line 3" );

$ sed '/^XYZ/, /);$/ d' input

hello world

EDIT

To take care of cases where the function occures in same line

$ sed '/^XYZ.*);$/d;  /^XYZ/, /);$/ d' input

Test

$ cat input
XYZ( "line 1",
     "line 2",
     "line 3" );
helo
XYZ( "line 1",     "line 2",     "line 3" );
hello world

$ sed '/^XYZ.*);$/d;  /^XYZ/, /);$/ d' input
helo
hello world

Upvotes: 1

Related Questions