Back again with some more advice.
You might consider using
ed or
vim instead. As dedicated text editors, they can update the file directly without the need to work through temporary files. All you need to do is feed them a series of commands separated by newline characters (I like to use
printf for this personally).
ed is lighter and more portable, but it has the downside that you can't change the "
s" delimiter, so we're back to some picket-fence like strings.
Code:
printf '%s\n' 'g/DROP .*;/ s/.*/\/* & *\//' ',p' | ed -s file.txt
"
,p" just prints the modified file to stdout. Change it to "
w" to write the changes back to the original file.
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
vim works in an nearly identical fashion, and it
can use different delimiters (although not my favorite "
|", which is reserved as a command separator.
, so I'm using "
@" in my example below).
Code:
printf '%s\n' 'g/DROP .*;/ s@.*@/* & */@' '%p' | vim -es file.txt
Again, change "
%p" to "
w" to write the changes back to file.
I'm still a bit of a newbie regarding vim though, so I'm not 100% sure I have the best syntax for the command.