Uncomment Lines in a File using SED in Linux

|
Last Updated:
|
|

In this tutorial, you will learn how to uncomment lines in a file using SED in Linux. According to man sed, “Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors“.

Uncommenting Lines in a File using SED in Linux

So, instead of opening a file and uncommenting a specific line, you would simply save some time by just using sed from the command line to uncomment that specific line.

In order to demonstrate how to use sed to uncomment lines in a file using in linux, take for example you have a file with the contents below;

cat /tmp/lines
This is line1
#This is line2
This is line3
#line4
This is line5
This is line6
#My line7
This is line8
This is line9
This is line10
This is line11
Your is line12
This is line13
This is line14
#Their line15
This is line16
This is line17
#This is line18
This is line19
This is line20

A number of lines have been commented (# placed at the beginning).

Uncomment a line with a specific pattern using SED in Linux

To uncomment a specific line with a specific pattern using sed, you would simply run the command below;

sed '/pattern/s/^#//' -i file

The above will only ucomment a first matching line.

Replace the pattern with the matching keyword of the line and file with specific file name.

If the file contains multiple lines matching the pattern and all of them are commented and want to uncomment them all, ensure you run the sed command globally using the g operation;

sed '/pattern/s/^#//g' -i file

For example, to uncomment a line with the pattern This, then run;

sed '/This/s/^#//' -i /tmp/lines

If you want to run set in dry run mode without actually applying the changes to inline file, then omit option -i.

The above command will only uncomment the first commented line matching the specified pattern, which in this case is the second line, #This is line2.

cat /tmp/lines
This is line1
This is line2
This is line3
#line4
This is line5
This is line6
#My line7
This is line8
This is line9
This is line10
This is line11
Your is line12
This is line13
This is line14
#Their line15
This is line16
This is line17
#This is line18
This is line19
This is line20

To uncomment all the commented lines in the file matching the pattern, This, then run;

sed '/This/s/^#//g' -i /tmp/lines

If you want to create a backup of the original file before applying the changes, then use the option -i.<extension> instead of -i where <extension> can be bak, original, old or whatever preffix to append to the backup file.

sed '/This/s/^#//g' -i.bak /tmp/lines

Uncomment specific line number in a file using SED in Linux

It is also possible to use sed to uncomment specific line number in a file.

In a file, while using vim editor, you can display line numbers by pressing ESC, and entering, :set number;

So taking for example, you want to uncomment line number 4 in a file, then run sed as follows;

sed '4s/^#//' /tmp/lines
This is line1
This is line2
This is line3
line4
This is line5
This is line6
#My line7
...

To apply the changes in the file, use option -i as already shown above.

To uncomment more than one lines (range of lines), then you can specify the line number to begin and where to end on the sed command;

sed '$LINESTART,$LINEENDs/^#//' file

For example, to uncomment the lines from line number 4 through line number 7, then;

sed '4,7s/^#//' file

What if the lines have some identation such that they begin with tabs or white spaces instead, like;

This is line1
	#This is line2
This is line3
    #line4
This is line5
This is line6

then you can run;

sed '1,5s/.*#//' /tmp/lines

This will remove the #, the white spaces or tabs. Sample result.

This is line1
This is line2
This is line3
line4
This is line5
This is line6
#My line7

So to keep the identation, then you can simply remove the #;

sed '1,5s/#//' /tmp/lines

Sample result;

This is line1
	This is line2
This is line3
    line4
This is line5
This is line6
#My line7

Indentation is kept.

If you have some more examples, feel free to leave a comment.

Other tutorials

How to copy paste lines in vim

Make Permanent DNS Changes on resolv.conf in Linux

Check directory usage with du Command in Linux

How to use htop Command in Linux

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment