The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2008
cooldude cooldude is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 44
Perl: Getting back reference from s modifier

My input text has the following pattens:
Code:
  func_a(3,
         4,
         5);
I want to replace it with this:
Code:
   
  func_b(3,
         4,
         5,
         6);
I'm trying the following expression, but it does not work:
Code:
perl -p -e "s/func_a\((.*)?\);/func_b(\1,\n6)/s" < file |more
Any ideas? Thanks.
  #2 (permalink)  
Old 03-17-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
use $1 and $6 instead of \1 and \6, see if that helps. Use single-quotes around the code unless you are on windows.
  #3 (permalink)  
Old 03-18-2008
cooldude cooldude is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 44
Thanks, but that does not work. By the way, I'm not using \6, but rather \n6.
  #4 (permalink)  
Old 03-18-2008
rajut1984 rajut1984 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
Use $1 instead of \1

Modify the command as below ...
perl -p -e "s/func_a\((.*)?\);/func_b\($1,\n6\);/s" < file |more
  #5 (permalink)  
Old 03-18-2008
cooldude cooldude is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 44
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);
Doing
Code:
perl -p -e "s/func_a\((.*)?\);/func_b($1,\n6)/s" < file |more
works as expected.
  #6 (permalink)  
Old 03-18-2008
era
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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.
  #7 (permalink)  
Old 03-18-2008
cooldude cooldude is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 44
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
and
Code:
perl -e "BEGIN {$/=undef} s/func_a\((.*)?\);/func_b(\1,\n6)/s"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0