![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl script to scan back lines | gholdbhurg | Shell Programming and Scripting | 3 | 03-18-2008 12:33 PM |
| Reference: WebObjects 5.4.1 Reference | iBot | UNIX and Linux RSS News | 0 | 03-11-2008 09:50 PM |
| Clipboard Modifier 0.2.0 (Default branch) | iBot | Software Releases - RSS News | 0 | 01-02-2008 05:00 PM |
| back reference error | apoorvasharma80 | Shell Programming and Scripting | 4 | 08-22-2006 10:25 AM |
| Reference two dimensional array in Perl sub | photon | Shell Programming and Scripting | 5 | 04-16-2003 04:52 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Perl: Getting back reference from s modifier
My input text has the following pattens:
Code:
func_a(3,
4,
5);
Code:
func_b(3,
4,
5,
6);
Code:
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: Code:
func_a(3); Code:
perl -p -e "s/func_a\((.*)?\);/func_b($1,\n6)/s" < file |more |
|
||||
|
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: Code:
perl -e -0777 "s/func_a\((.*)?\);/func_b(\1,\n6)/s" < file |more Code:
perl -e "BEGIN {$/=undef} s/func_a\((.*)?\);/func_b(\1,\n6)/s"
|
| Sponsored Links | ||
|
|