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

FAQ
It is currently Mon Dec 09, 2019 12:33 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 - SED uncomment line - How to uncomment a line or all lines with SED
Author Message
Post subject: SED uncomment line - How to uncomment a line or all lines with SED  |  Post Posted: Wed Jan 14, 2015 5:08 pm
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.
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