473,388 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Find and Replace with Perl

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
12 3791
numberwhun
3,509 Expert Mod 2GB
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
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
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
4,059 Expert 2GB
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
Thanks for the quick reply Kevin...I will give this a try... :)
Jan 27 '08 #6
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
4,059 Expert 2GB
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
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
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
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
DUH! Found that I just needed to escape the special characters in the strings.

Now it works great...persistence.
Jan 27 '08 #12
KevinADC
4,059 Expert 2GB
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

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

Similar topics

9
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
1
by: Karen | last post by:
I am very new to MySQL- I currently have an VBA module using a VBSCript that will find different aspects of a text string that are a unique text string and turn it into a not so unique text string....
4
by: tungchau81 | last post by:
Hi, I have a lot of JSP files that serve as pure JavaScript code. I need to convert all JavaScript comments inside these JSP files to JSP comments which use <%-- // --%> tags. Instead of doing it...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
8
by: John Pye | last post by:
Hi all I have a file with a bunch of perl regular expressions like so: /(^|)\*(.*?)\*(|$)/$1'''$2'''$3/ # bold /(^|)\_\_(.*?)\_\_(|$)/$1''<b>$2<\/ b>''$3/ # italic bold...
6
by: alainfri | last post by:
I am not sure if this group is the right place for this question but what I need is as follows. There is a piece of html. Throughout the html there are a lot of <brtags. The task is to replace all...
6
by: erbrose | last post by:
Hey all! been searching around the forum and google this morning looking for a simple way to parse thru a tab delinated file and do a find and replace. Here is what I've come up with so far as a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.