SW Engineer wrote in post #1171014:
I have a text file that contains file paths as follows:
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
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
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