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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl one line search and replace based on a condition

 



blrsmani
New User

Sep 14, 2010, 5:00 AM

Post #1 of 5 (1757 views)
Perl one line search and replace based on a condition Can't Post

I have a one liner which will replace a string globally as below:

find . -name "*.xml" -type f -exec perl -i -ne 's/'=\'934'/'=\'215'/g;print;' {} \;

but instead of replacing in all lines, I want to replace only if the line contains " ='ad1' " string.

thank you,

Mani


(This post was edited by blrsmani on Sep 14, 2010, 5:01 AM)


1arryb
User

Sep 14, 2010, 8:49 AM

Post #2 of 5 (1748 views)
Re: [blrsmani] Perl one line search and replace based on a condition [In reply to] Can't Post

Hi blrsmani,

Try this:

Code
# Note: Perl regular expressions are are quoted by default. 
find . -name "*.xml" -type f -exec perl -i -ne 's/=934/=215/g if /ad1/; print;' {} \;

Cheers,

Larry


(This post was edited by 1arryb on Sep 15, 2010, 11:46 AM)


blrsmani
New User

Sep 15, 2010, 2:23 AM

Post #3 of 5 (1741 views)
Re: [1arryb] Perl one line search and replace based on a condition [In reply to] Can't Post

Larry,
Thank you for your reply.

I have the digits starting with =' in the xml files. So, I have used s/=\' below.

This piece of code waits for some more input on the console.

Code
find . -name "*.xml" -type f -exec perl -i -ne 's/=\'934/=\'214/g if /ad1/; print;' {} \;


This piece of line is deleting all the xml files contents in all sub directories.

Code
find . -name "*.xml" -type f -exec perl -i -ne 's/'=\'934'/'=\'214'/g if /ad1/; print;' {} \;


thank you,
mani


(This post was edited by blrsmani on Sep 15, 2010, 2:24 AM)


1arryb
User

Sep 15, 2010, 9:11 AM

Post #4 of 5 (1729 views)
Re: [blrsmani] Perl one line search and replace based on a condition [In reply to] Can't Post

Hi blrsmani,

I tested the code I presented in my previous reply. It did neither of the things you complain of. Why don't you try that?

Update: My one-liner was split onto 2 lines by the perlguru forum software. Fixed now.

Cheers,

Larry


(This post was edited by 1arryb on Sep 15, 2010, 11:48 AM)


blrsmani
New User

Sep 16, 2010, 12:20 AM

Post #5 of 5 (1703 views)
Re: [1arryb] Perl one line search and replace based on a condition [In reply to] Can't Post

Thank you so much, Larry.

I did not include the = (equal to) character for replacement, it worked. :-O


Code
find . -name "*.xml" -type f -exec perl -i -ne 's/'134/'218/g if /ad2/; print;' {} \;


Thanks a lot again.

blrsmani

 
 


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

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