CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Substitution Issues


New User

Oct 18, 2011, 3:10 PM

Post #1 of 4 (1038 views)
Substitution Issues Can't Post

Hey everyone,

I have peeked around a bit for answers regarding substitution problems and I haven't run in to any that cover the issue that I seem to be having. I am searching a number of text documents in a folder for specific strings and removing them. One of the strings in question is as follows:

"SOURCE NCHAR(20)  NOT NULL  SET("Description" = ''), "

To replace this I attempted to use the following line:

find /cygdrive/c/ -type f | xargs perl -pi -e 's/SOURCE NCHAR(20)  NOT NULL  SET(\"Description\" = '\'''\''), //gi'

The backup gets generated accordingly and no runtime errors occur; the text files however do not change in any way.

I assumed that I may have been missing something in the string to be replaced so I ran the following to make sure that I was indeed feeding the correct string to the command:

perl -e 'print "SOURCE NCHAR(20)  NOT NULL  SET(\"Description\" = '\'''\''),"'

The above command prints a line identical to the one I wish to replace:

"SOURCE NCHAR(20)  NOT NULL  SET("Description" = ''), "

Lastly I thought that my text files may have hidden characters or something wrong with the encoding, so I created a temp.txt and placed only the string I wished replaced inside of it and then ran the substitute command and the string still remained.

I assume that I am missing something simple but alas I cannot see it. Any light you guys could shed on this issue would be much appreciated.


Veteran / Moderator

Oct 18, 2011, 4:15 PM

Post #2 of 4 (1032 views)
Re: [StackOverflow] Substitution Issues [In reply to] Can't Post


find /cygdrive/c/ -type f | xargs perl -pi -e 's/SOURCE NCHAR\(20\)  NOT NULL  SET\("Description" = \'\'\), //gi'

New User

Oct 18, 2011, 5:30 PM

Post #3 of 4 (1027 views)
Re: [FishMonger] Substitution Issues [In reply to] Can't Post

FishMonger, thanks for the quick response.

I tried using your aforementioned command and I was left with a '>' prompt in the shell as if it were waiting for more input. I had tried escaping parentheses earlier today and found that in some cases it seemed to be needed and others it was not. I will keep muddling with it.

*EDIT* Aha thanks to your input I took my original command and applied escape backslashes to the parentheses.

find /cygdrive/c/ -type f | xargs perl -pi -e 's/SOURCE NCHAR\(20\)  NOT NULL  SET\(\"Description\" = '\'''\''\), //gi'

This removed the text beautifully!

Thanks again for your input that led me down the right path!Smile

(This post was edited by StackOverflow on Oct 18, 2011, 5:36 PM)

New User

Oct 21, 2011, 6:02 PM

Post #4 of 4 (1000 views)
Re: [StackOverflow] Substitution Issues [In reply to] Can't Post

Having had the above solution work in my one-liner. I quickly found a need to expand to matching groups to fulfill some of my requirements; however I seem to be running in to a snag.

I have searched over what seems to be a majority of knowledge bases regarding regular expressions being used inside the PATTERN portion of a substitution and I find myself attempting the following:

String I need to replace:

 SOURCE varchar(20) SET("ui_mapping_text"='\'Rapid Decision 5.3\' 

') ,

Substitution Code I am using:

s/^[ ]+SOURCE.*\s*.*,/''/igm

In my Perl script, I am actually looping through substitutions whose patterns are in a string array to remove specific strings in a text file. I have found that when recording a regular expression as a string you need to take backslashes in to account carefully so that at runtime the expression gets read correctly.

With that being said, does anyone see any backslashes off in the pattern? I have tried escaping many combinations of the whitespace character and the dots, etc. I just can't seem to find the right combination.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives