Home: Perl Programming Help: Beginner:
replace path(string) inside scripts



raopatwari
Novice

Aug 4, 2014, 4:44 AM


Views: 1288
replace path(string) inside scripts

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


Views: 1279
Re: [raopatwari] replace path(string) inside scripts

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


Views: 1277
Re: [raopatwari] replace path(string) inside scripts

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


Views: 1274
Re: [raopatwari] replace path(string) inside scripts

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
Novice

Aug 4, 2014, 12:40 PM


Views: 1266
Re: [Laurent_R] replace path(string) inside scripts

Thank you very much, That was helpful