I have a text file that contains file paths as follows:
I want to replace the string “drug” by “med” wherever it occurs.
I used something like the following:
gsub!(/(drug)/,‘medication’) if contents.include? ‘drug’
(/(drug)/ is supposed to be a regular expression, but, I may be writing
it wrongly.
Do you know what I can do to perform such task?
Thanks.
SW Engineer wrote in post #1171014:
I have a text file that contains file paths as follows:
http://www.accessdata.fda.gov/drugsatfda_docs/label/1998/11961lbl.pdf
That is not a file path but a URL.
I want to replace the string “drug” by “med” wherever it occurs.
I used something like the following:
gsub!(/(drug)/,‘medication’) if contents.include? ‘drug’
The check with “if” is superfluous as #gsub will only replace if there
are matches. An additional check will only make it slower.
(/(drug)/ is supposed to be a regular expression, but, I may be writing
it wrongly.
The expression is OK (apart from brackets which are not needed here). On
what object are you invoking #gsub!?
Do you know what I can do to perform such task?
If you have the file contents in a variable called “contents” then you
can simply do
contents.gsub!(/drugs/, ‘medication’)
A simpler solution would be to use sed on the command line:
sed ‘s#drugs#medication#g’ file
You can even do that in place
sed -i.bak ‘s#drugs#medication#g’ file
Cheers
robert