Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

Using sed to comment/uncomment lines in files
https://forum.ivorde.com/using-sed-to-comment-uncomment-lines-in-files-t72.html
Page 1 of 1

Author:  debuser [ Fri Apr 02, 2010 6:07 am ]
Post subject:  Using sed to comment/uncomment lines in files

Using sed to comment or uncomment lines in files:

The following test file has a commented line inside it.
Code:
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple
#commands may be specified by using the -e or -f options.  All commands
are applied to the input in the order they are specified regardless of
their origin.


Uncomment a line in a file with sed:
Code:
~ sed -i '' 's/^#commands/commands/g' man.sed
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple
commands may be specified by using the -e or -f options.  All commands
are applied to the input in the order they are specified regardless of
their origin.


Comment a line in a file with sed:
Code:
~ sed -i '' 's/^commands/#commands/g' man.sed
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple
#commands may be specified by using the -e or -f options.  All commands
are applied to the input in the order they are specified regardless of
their origin.


Commenting all lines of a file with sed:
Code:
~ sed -i '' 's/\(.*\)/#\1/g' man.sed
~ cat man.sed
#The sed utility reads the specified files, or the standard input if no
#files are specified, modifying the input as specified by a list of com-
#mands.  The input is then written to the standard output.
#
#A single command may be specified as the first argument to sed.  Multiple
#commands may be specified by using the -e or -f options.  All commands
#are applied to the input in the order they are specified regardless of
#their origin.

The above command for commenting all lines is not quite correct because, in case there are lines already commented, they will be double commented. In some cases this can be useful (to know what to undo) or not.

Here's how to remove all the comments with sed:
Code:
~ sed -i '' 's/^#\(.*\)/\1/g' man.sed
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple
commands may be specified by using the -e or -f options.  All commands
are applied to the input in the order they are specified regardless of
their origin.


Below is an example of commenting only lines that aren't already commented in a file:
Code:
~ cat man.sed
The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands.  The input is then written to the standard output.

A single command may be specified as the first argument to sed.  Multiple
#commands may be specified by using the -e or -f options.  All commands
are applied to the input in the order they are specified regardless of
their origin.
~ sed -i '' 's/^\([^#]\)/#\1/g' man.sed
~ cat man.sed
#The sed utility reads the specified files, or the standard input if no
#files are specified, modifying the input as specified by a list of com-
#mands.  The input is then written to the standard output.

#A single command may be specified as the first argument to sed.  Multiple
#commands may be specified by using the -e or -f options.  All commands
#are applied to the input in the order they are specified regardless of
#their origin.


This is mostly useful when you're telneted over a serial connection where VI is a pain.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/