Perl: Getting back reference from s modifier

My input text has the following pattens:

  func_a(3,
         4,
         5);

I want to replace it with this:

   
  func_b(3,
         4,
         5,
         6);

I'm trying the following expression, but it does not work:

perl -p -e "s/func_a\((.*)?\);/func_b(\1,\n6)/s" < file |more

Any ideas? Thanks.

use $1 and $6 instead of \1 and \6, see if that helps. Use single-quotes around the code unless you are on windows.

Thanks, but that does not work. By the way, I'm not using \6, but rather \n6.

Use $1 instead of \1

Modify the command as below ...
perl -p -e "s/func_a\((.*)?\);/func_b\($1,\n6\);/s" < file |more

Yes, I used $1 instead of \1. I think it makes no difference.
When I have patterns that don't have new lines in them, such as:

  func_a(3);

Doing

perl -p -e "s/func_a\((.*)?\);/func_b($1,\n6)/s" < file |more

works as expected.

The problem is that with perl -p, you are only reading and examining a line at a time. Thus the pattern match is not being applied to anything which straddles a line boundary, even if you have the /s modifier. You can fix that by reading all lines at once, with perl -0777 or something equivalent.

See also the Perl FAQ, which has a detailed discussion of this topic.

perl should have given me an error/warning because the s modifier isn't applicable to -p.
Anyway, here's what I tried, but this time I get nothing outputted:

perl -e -0777 "s/func_a\((.*)?\);/func_b(\1,\n6)/s" < file |more

and

perl -e "BEGIN {$/=undef} s/func_a\((.*)?\);/func_b(\1,\n6)/s"

You dropped the -p so you're not reading your input any longer. Also note that the -e needs to go last, it says "the script is not in a file; the next argument is the script".

perl -0777 -p -e 's/func_a\((.*)?\);/func_b(\1,\n6)/s' < file | more

Thanks. I had to add the global replace modifier and the put the non-greedy modifier inside the grouping.

perl -0777 -p -e 's/func_a\((.*?)\);/func_b(\1,\n         6);/gs' < file