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: Beginner:
replace path(string) inside scripts

 



raopatwari
New User

Aug 4, 2014, 4:44 AM

Post #1 of 5 (651 views)
replace path(string) inside scripts Can't Post

I am totally new to perl scripting..


I have to write a script which will scan thru all the scripts in particular directory, it has to replace the the path inside the scipts...


if it finds the the path [/production/tep/scripts/] then it should be replaced by [/development0/tep/scripts/]

elsif [/iris/data2/data_source/] then it should be replaced by [/data/tep/development0/iris/data2/data_source/]

elsif [/production/data3/data_source_history/] then it should be replaced by [/data/tep/development0/iris/data2/data_source_history/]

elsif [/logs/tep/production/scripts/] then it should be replaced by [/logs/tep/development0/scripts/]..


One sample is script is shown below.............




data_source: /iris/data2/data_source/ams_outputs/
data_source_history: /production/data3/data_source_history/ams_outputs/
tmp_file: /tmp/ams_outputs_n.txt
log_file: /logs/tep/production/scripts/ams_outputs_wmx.log
lock_file: /logs/tep/production/scripts/ams_outputs_wmx.lockfile
executable: /production/tep/scripts/ams_outputs_wmx
loader: sprint_usage.tep_ams_outputs.main



your help is rally appreciated. Thanks in advanced


Laurent_R
Veteran / Moderator

Aug 4, 2014, 10:08 AM

Post #2 of 5 (642 views)
Re: [raopatwari] replace path(string) inside scripts [In reply to] Can't Post

Please do not post the same question in different sections of the forum. I'll remove th eon you've put in the intermediate section since you said yourself that you are a beginner.


Laurent_R
Veteran / Moderator

Aug 4, 2014, 10:20 AM

Post #3 of 5 (640 views)
Re: [raopatwari] replace path(string) inside scripts [In reply to] Can't Post

To do it on one file:

Code
perl -pi.bak -e ' 
s{/production/tep/scripts/}{/development0/tep/scripts/}g;
# same syntax for other substitutions to be made
' file_name.txt

To do it on multiple files, make another script that will call the above with each file name.

Make sure you test on a copy of the data, some errors might lead to loss of data.


(This post was edited by Laurent_R on Aug 4, 2014, 10:36 AM)


Laurent_R
Veteran / Moderator

Aug 4, 2014, 10:40 AM

Post #4 of 5 (637 views)
Re: [raopatwari] replace path(string) inside scripts [In reply to] Can't Post

Actually, if you are using Unix/Linux and bash, it can even be simpler, you don't need another script. This:


Code
perl -pi.bak -e '  
s{/production/tep/scripts/}{/development0/tep/scripts/}g;
# same syntax for other substitutions to be made
' path_to_files/*.*

will make all the substitutions in all the files in the path_to_files directory.

(BTW, it will also create a non-modified copy of each file with the '.bak' extension.)


raopatwari
New User

Aug 4, 2014, 12:40 PM

Post #5 of 5 (629 views)
Re: [Laurent_R] replace path(string) inside scripts [In reply to] Can't Post

Thank you very much, That was helpful

 
 


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

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