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

FAQ
It is currently Fri Jun 02, 2023 3:58 am


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

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

Joined: Thu Aug 06, 2009 2:48 am
Posts: 105

Offline
 

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.





Top
Display posts from previous:  Sort by  
E-mail friendPrint 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

2257

Mon Apr 26, 2010 11:06 am

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

8828

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. Linux / FreeBSD: Avoid bash history duplicate lines with HISTCONTROL

mandrei99

0

2561

Mon Jan 12, 2015 12:01 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Echo new line in linux - supress new line or echo multiple new lines

mandrei99

0

6018

Wed Jan 14, 2015 5:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. SED uncomment line - How to uncomment a line or all lines with SED

mandrei99

0

7765

Wed Jan 14, 2015 5:08 pm

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 1 guest
You can post new topics in this forum
You can 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:  
cronNews 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