Is there a way to comment/uncomment a shell/config/ruby script using command line?
for example:
$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf
this would add or remove #
sign on bla.conf
on line 14
to 18
. Normally I use sed
, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).
Best Answer
To comment lines 2 through 4 of bla.conf:
To make the command that you wanted, just put the above into a shell script called comment:
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
An uncomment command can be created analogously.
Advanced feature
sed
's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containingfoo
to the one containingbar
, use:BSD (OSX) Systems
With BSD sed, the
-i
option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:And, replace the command in the script with: