{"id":8833,"date":"2021-05-14T21:13:24","date_gmt":"2021-05-14T18:13:24","guid":{"rendered":"https:\/\/kifarunix.com\/?p=8833"},"modified":"2024-03-18T22:26:55","modified_gmt":"2024-03-18T19:26:55","slug":"uncomment-lines-in-a-file-using-sed-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/uncomment-lines-in-a-file-using-sed-in-linux\/","title":{"rendered":"Uncomment Lines in a File using SED in Linux"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to uncomment lines in a file using SED in Linux. According to <strong>man sed<\/strong>, &#8220;<em>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&#8217;s ability to filter text in a pipeline which particularly distinguishes it from other types of editors<\/em>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Uncommenting Lines in a File using SED in Linux<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cat \/tmp\/lines<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\n#This is line2\nThis is line3\n#line4\nThis is line5\nThis is line6\n#My line7\nThis is line8\nThis is line9\nThis is line10\nThis is line11\nYour is line12\nThis is line13\nThis is line14\n#Their line15\nThis is line16\nThis is line17\n#This is line18\nThis is line19\nThis is line20<\/code><\/pre>\n\n\n\n<p> A number of lines have been commented (# placed at the beginning).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Uncomment a line with a specific pattern using SED in Linux<\/h3>\n\n\n\n<p>To uncomment a specific line with a specific pattern using sed, you would simply run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '\/pattern\/s\/^#\/\/' -i file<\/code><\/pre>\n\n\n\n<p>The above will only ucomment a first matching line.<\/p>\n\n\n\n<p>Replace the <strong>pattern<\/strong> with the <strong>matching keyword<\/strong> of the line and <strong>file<\/strong> with specific file name.<\/p>\n\n\n\n<p>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 <strong>g<\/strong> operation;<\/p>\n\n\n\n<pre id=\"block-b0d26c2f-97af-46d3-be08-81a08f195117\" class=\"wp-block-preformatted\">sed '\/pattern\/s\/^#\/\/g' -i file<\/pre>\n\n\n\n<p>For example, to uncomment a line with the pattern <strong>This<\/strong>, then run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '\/This\/s\/^#\/\/' -i \/tmp\/lines<\/code><\/pre>\n\n\n\n<p>If you want to run set in dry run mode without actually applying the changes to inline file, then omit option <strong><code>-i<\/code><\/strong>.<\/p>\n\n\n\n<p>The above command will only uncomment the first commented line matching the specified pattern, which in this case is the second line, <strong>#This is line2<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cat \/tmp\/lines<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\nThis is line2\nThis is line3\n#line4\nThis is line5\nThis is line6\n#My line7\nThis is line8\nThis is line9\nThis is line10\nThis is line11\nYour is line12\nThis is line13\nThis is line14\n#Their line15\nThis is line16\nThis is line17\n#This is line18\nThis is line19\nThis is line20<\/code><\/pre>\n\n\n\n<p>To uncomment all the commented lines in the file matching the pattern, <strong>This<\/strong>, then run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '\/This\/s\/^#\/\/g' -i \/tmp\/lines<\/code><\/pre>\n\n\n\n<p>If you want to create a backup of the original file before applying the changes, then use the option <strong>-i.&lt;extension&gt;<\/strong> instead of <strong>-i<\/strong> where <strong>&lt;extension&gt;<\/strong> can be <strong>bak<\/strong>, original, old or whatever preffix to append to the backup file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '\/This\/s\/^#\/\/g' -i.bak \/tmp\/lines<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Uncomment specific line number in a file using SED in Linux<\/h3>\n\n\n\n<p>It is also possible to use sed to uncomment specific line number in a file.<\/p>\n\n\n\n<p>In a file, while using vim editor, you can display line numbers by pressing <strong>ESC,<\/strong> and entering, <strong>:set number<\/strong>;<\/p>\n\n\n\n<p>So taking for example, you want to uncomment line number 4 in a file, then run sed as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '4s\/^#\/\/' \/tmp\/lines<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\nThis is line2\nThis is line3\nline4\nThis is line5\nThis is line6\n#My line7\n...<\/code><\/pre>\n\n\n\n<p>To apply the changes in the file, use option <strong>-i<\/strong> as already shown above.<\/p>\n\n\n\n<p>To uncomment more than one lines (<strong>range of lines<\/strong>), then you can specify the line number to begin and where to end on the sed command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '$LINESTART,$LINEENDs\/^#\/\/' file<\/code><\/pre>\n\n\n\n<p>For example, to uncomment the lines from line number 4 through line number 7, then;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '4,7s\/^#\/\/' file<\/code><\/pre>\n\n\n\n<p>What if the lines have some identation such that they begin with tabs or white spaces instead, like;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\n\t#This is line2\nThis is line3\n    #line4\nThis is line5\nThis is line6<\/code><\/pre>\n\n\n\n<p>then you can run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '1,5s\/.*#\/\/' \/tmp\/lines<\/code><\/pre>\n\n\n\n<p>This will remove the #, the white spaces or tabs. Sample result.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\nThis is line2\nThis is line3\nline4\nThis is line5\nThis is line6\n#My line7<\/code><\/pre>\n\n\n\n<p>So to keep the identation, then you can simply remove the #;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed '1,5s\/#\/\/' \/tmp\/lines<\/code><\/pre>\n\n\n\n<p>Sample result;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is line1\n\tThis is line2\nThis is line3\n    line4\nThis is line5\nThis is line6\n#My line7<\/code><\/pre>\n\n\n\n<p>Indentation is kept.<\/p>\n\n\n\n<p>If you have some more examples, feel free to leave a comment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other tutorials<\/h3>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-copy-paste-lines-in-vim\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to copy paste lines in vim<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/make-permanent-dns-changes-on-resolv-conf-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Make Permanent DNS Changes on resolv.conf in Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/check-directory-usage-with-du-command-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Check directory usage with du Command in Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-use-htop-command-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to use htop Command in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to uncomment lines in a file using SED in Linux. According to man sed, &#8220;Sed is a stream<\/p>\n","protected":false},"author":1,"featured_media":8843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[49,121],"tags":[3537,3536,3538,3539,3540],"class_list":["post-8833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-cheatsheets","category-howtos","tag-sed-uncomment-lines","tag-uncomment-lines-in-a-file-using-sed-in-linux","tag-uncomment-range-of-lines-sed","tag-uncomment-specific-line-sed","tag-uncomment-without-changing-identation-with-sed","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8833"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=8833"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8833\/revisions"}],"predecessor-version":[{"id":21815,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8833\/revisions\/21815"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/8843"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=8833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=8833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=8833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}