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

FAQ
It is currently Sun Sep 24, 2017 6:15 pm


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

Author Message
mandrei99
Post  Post subject: SED uncomment line - How to uncomment a line or all lines with SED  |  Posted: Wed Jan 14, 2015 5:08 pm

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
 

SED uncomment line - How to uncomment a line or all lines with SED

Sed is a very powerful linux command line tool. It is able to replace either static characters or based on regular expressions.

I will give here a few examples on how to uncomment all lines or specific line based on regular expression.

Sed uncomment all lines in a file:
Code:
user@linux:~# cat file.conf
line1
#line2
line3A
#setnence abc
sentence def
#sentence bcd
user@linux:~# sed 's/^#\(.*\)/\1/g' file.conf   
line1
line2
line3A
setnence abc
sentence def
sentence bcd


Sed uncomment specific line:
Code:
user@linux:~# cat file.conf
line1
#line2
line3A
#setnence abc
sentence def
#sentence bcd
user@linux:~# sed 's/^#line2/line2/g' file.conf             
line1
line2
line3A
#setnence abc
sentence def
#sentence bcd


Sed uncomment specific lines based on regular expression - uncomment lines containing bc combination:
Code:
user@linux:~# cat file.conf                     
line1
#line2
line3A
#setnence abc
sentence def
#sentence bcd
user@linux:~# sed 's/^#\(.*\)bc\(.*\)/\1bc\2/g' file.conf               
line1
#line2
line3A
setnence abc
sentence def
sentence bcd



All the above examples operate on a memory copy of the file, not on the file on the disk. Use "-i" switch of sed to uncomment lines in the file.
Quote:
-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied)

Code:
user@linux:~# cat file.conf                     
line1
#line2
line3A
#setnence abc
sentence def
#sentence bcd
user@linux:~# sed -i 's/^#\(.*\)bc\(.*\)/\1bc\2/g' file.conf
user@linux:~# cat file.conf
line1
#line2
line3A
setnence abc
sentence def
sentence bcd


More on sed replacement based on regular expressions:
- \(.*\) represents a regular expression matching group for any characters zero or multiple times.
- \1 and \2 represent the first and second regex matching groups in the match criteria. They are used in the replacement part of sed command.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "SED uncomment line - How to uncomment a line or all lines with SED"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Using sed to comment/uncomment lines in files

debuser

0

5229

Fri Apr 02, 2010 6:07 am

debuser 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

2852

Wed Jan 14, 2015 5:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Remove empty lines in vi/vim

debuser

0

547

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

4941

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

573

Mon Jan 12, 2015 12:01 pm

mandrei99 View the latest post

There are no new unread posts for this topic. How to replace new line in perl script

mandrei99

1

1851

Wed Jun 26, 2013 8:18 am

admin View the latest post

There are no new unread posts for this topic. How to convert a text into a list of words, one per line

LaR3

0

4599

Wed Aug 05, 2009 7:02 am

LaR3 View the latest post

There are no new unread posts for this topic. How to replace \w SED multiple matches on the same line without piping

mandrei99

0

1095

Tue Oct 08, 2013 6:29 am

mandrei99 View the latest post

There are no new unread posts for this topic. Removing new line / EoL characters in a PERL script

mandrei99

0

2358

Mon Nov 04, 2013 6:47 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to add line numbers in unix shell output

mandrei99

0

878

Fri Oct 31, 2014 5:31 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
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:  
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