Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Fri Dec 14, 2018 12:08 am


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - Using sed to comment/uncomment lines in files
Author Message
Post subject: Using sed to comment/uncomment lines in files  |  Post Posted: Fri Apr 02, 2010 6:07 am
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.
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO