By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,839 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,839 IT Pros & Developers. It's quick & easy.

Find and Replace with Perl

P: 9
I am trying to execute a perl one-liner to find and replace text without much luck. I think it is because my strings have so many spaces.

This is the command:

find J:\testing\1 -name 'ap160ws' | xargs perl -pi -e 's/05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100\.$/05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000\./g'

I want to find: "05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100."

and replace: "05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000."


Any assistance would be greatly appreciated!
Jan 27 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I am trying to execute a perl one-liner to find and replace text without much luck. I think it is because my strings have so many spaces.

This is the command:

find J:\testing\1 -name 'ap160ws' | xargs perl -pi -e 's/05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100\.$/05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000\./g'

I want to find: 05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100.

and replace: 05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000.


Any assistance would be greatly appreciated!
This can be accomplished pretty easily with a replacement regular expression. Why not post the code you have tried and we will help you get it working?

Regards,

Jeff
Jan 27 '08 #2

P: 9
This is the command I have tried:

find J:\peg_download\ultraedit\testing -name 'ap160ws' | xargs perl -pi -e 's/05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100\.$/05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000\./g'


unfortunately...your forum squeezes all of the spaces out so I'm not sure that you will see the problem...
Jan 27 '08 #3

P: 9
I have also tried this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict; 
  4. use warnings; 
  5. use File::Find; 
  6.  
  7. my $startdir = 'J:\testing\1'; 
  8. my $find = '05  AP520WS-ENTRY-CNT-MAX      PIC 9(03)    VALUE 100.'; 
  9. my $replace = '05  AP520WS-ENTRY-CNT-MAX      PIC 9(04)    VALUE 1000'; 
  10. my $doctype = 'file';  
  11.  
  12. print qq~Finding "$find" and replacing it with "$replace"\n~; 
  13.  
  14. find( 
  15.    sub{ 
  16.       return unless (/\.$doctype$/i); 
  17.       local @ARGV = $_; 
  18.       local $^I = '.bac'; 
  19.       while( <> ){ 
  20.            if( s/$find/$replace/ig ) { 
  21.               print; 
  22.             } 
  23.             else { 
  24.                print; 
  25.             } 
  26.        } 
  27. }, $startdir);
  28.  
  29. print "Finished";

...it runs with no errors but does not create the .bac file so obviously not getting a hit on the search string. I'm flummoxed!

Thanks for any assistance!
Jan 27 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Your search pattern contains a number of special characters that perl uses internally like () for capturing patterns into memory. You may just need to use \Q...\E to escape all the non-word characters in the search pattern::

Expand|Select|Wrap|Line Numbers
  1. s/\Q05 AP520WS-ENTRY-CNT-MAX PIC 9(03) VALUE 100.\E$/05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000\./g'
Jan 27 '08 #5

P: 9
Thanks for the quick reply Kevin...I will give this a try... :)
Jan 27 '08 #6

P: 9
Unfortunately it modified it this way:

05\ \ AP520WS\-ENTRY\-CNT\-MAX\ \ \ \ \ \ PIC\ 9\(04\)\ \ \ \ VALUE\ 1000.


How can I make the replacement without all of the slashes?
Jan 27 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Unfortunately it modified it this way:

05\ \ AP520WS\-ENTRY\-CNT\-MAX\ \ \ \ \ \ PIC\ 9\(04\)\ \ \ \ VALUE\ 1000.


How can I make the replacement without all of the slashes?

The \Q modifier should not actually add the backslashes into the substitution. Try adding the back-slashes only where needed in the search pattern:

Expand|Select|Wrap|Line Numbers
  1. ind J:\testing\1 -name 'ap160ws' | xargs perl -pi -e 's/05 AP520WS\-ENTRY\-CNT\-MAX PIC 9\(03\) VALUE 100\./05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000./g'
see if that works better.
Jan 27 '08 #8

P: 9
The \Q modifier should not actually add the backslashes into the substitution. Try adding the back-slashes only where needed in the search pattern:

Expand|Select|Wrap|Line Numbers
  1. ind J:\testing\1 -name 'ap160ws' | xargs perl -pi -e 's/05 AP520WS\-ENTRY\-CNT\-MAX PIC 9\(03\) VALUE 100\./05 AP520WS-ENTRY-CNT-MAX PIC 9(04) VALUE 1000./g'
see if that works better.
Well...I am not sure where they are needed.
Jan 27 '08 #9

P: 9
This is what I used and got this error: Substitution pattern not terminated at -e line 1.

find J:\testing -name 'ap160ws' | xargs perl -pi -i.bak -e 's/\05 \AP520WS-ENTRY-CNT-MAX\ \PIC 9(03)\ \VALUE 100\/\05\ \AP520WS-ENTRY-CNT-MAX\ \PIC 9(04)\ \VALUE 1000\/g'


I am not sure where to place the slashes.
Jan 27 '08 #10

P: 9
I just checked a copy of the the "Perl Cookbook" and I think I see what you meant about escaping the non-word characters.

I'll give this another try!
Jan 27 '08 #11

P: 9
DUH! Found that I just needed to escape the special characters in the strings.

Now it works great...persistence.
Jan 27 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
Make sure you read replies thoroughly. My post above: http://www.thescripts.com/forum/post3045204-8.html showed you where to put the slashes. I posted the code, advised you to give it a try and to see how well it worked. Glad you got it figured out in the end.
Jan 27 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.