Ivorde Unix/Linux/Database/Web/Mail Forum

View unanswered posts
View active topics
It is currently Sat Feb 04, 2012 10:33 pm


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Bookmark this article:

Author Message
debuser
  Post  Post subject: Using sed to comment/uncomment lines in files  |  Posted: Fri Apr 02, 2010 1:07 pm

Joined: Thu Aug 06, 2009 9:48 am
Posts: 90

Offline
Save on Delicious
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.


Top
Display posts from previous:  Sort by  
Print view

Topics related to - "Using sed to comment/uncomment lines in files"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Remove empty lines in vi/vim

debuser

0

344

Mon Apr 26, 2010 6:06 pm

debuser View the latest post

There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

3724

Thu Feb 25, 2010 3:08 pm

debuser View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
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 + 2 hours [ DST ]

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
DAJ Glass 2 template created by Dustin Baccetti

phpBB SEO