{"id":1678,"date":"2013-07-17T00:18:17","date_gmt":"2013-07-17T00:18:17","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1678"},"modified":"2013-07-17T00:18:17","modified_gmt":"2013-07-17T00:18:17","slug":"delete-one-or-more-lines-from-a-file-using-sed","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1678","title":{"rendered":"Delete one or more lines from a file using SED"},"content":{"rendered":"<h1><\/h1>\n<p>Here is how to remove one or more lines from a file.<\/p>\n<p><b>Syntax:\u00a0<\/b><\/p>\n<pre>sed '{[\/]&lt;n&gt;|&lt;string&gt;|&lt;regex&gt;[\/]}d' &lt;fileName&gt;       \r\nsed '{[\/]&lt;adr1&gt;[,&lt;adr2&gt;][\/]d' &lt;fileName&gt;<\/pre>\n<ul>\n<li><b>\/&#8230;\/<\/b>=delimiters<\/li>\n<li><b>n\u00a0<\/b>= line number<\/li>\n<li><b>string<\/b>\u00a0= string found in in line<\/li>\n<li><b>regex\u00a0<\/b>= regular expression corresponding to the searched pattern<\/li>\n<li><b>addr<\/b>\u00a0= address of a line (number or pattern )<\/li>\n<li><b>d<\/b>\u00a0= delete<\/li>\n<\/ul>\n<p><b>Examples\u00a0<\/b><\/p>\n<p>Remove the 3rd line:<\/p>\n<pre>sed '3d' fileName.txt<\/pre>\n<p>Remove the line containing the string &#8220;awk&#8221;:<\/p>\n<pre>sed '\/awk\/d' filename.txt<\/pre>\n<p>Remove the last line:<\/p>\n<pre>sed '$d' filename.txt<\/pre>\n<p>Remove all empty lines:<\/p>\n<pre>sed '\/^$\/d' filename.txt       \r\nsed '\/.\/!d' filename.txt<\/pre>\n<p>Remove the line matching by a regular expression (by eliminating one containing digital characters, at least 1 digit, located at the end of the line):<\/p>\n<pre>sed '\/[0-9\/][0-9]*$\/d' filename.txt<\/pre>\n<p>Remove the interval between lines 7 and 9:<\/p>\n<pre>sed '7,9d' filename.txt<\/pre>\n<p>The same operation as above but replacing the address with parameters:<\/p>\n<pre>sed '\/-Start\/,\/-End\/d' filename.txt<\/pre>\n<p>The above examples are only changed at the display of the file (stdout1= screen).<\/p>\n<p>For permanent changes to the old versions (&lt;4) use a temporary file for GNU sed using the\u00a0<b>&#8220;-i[suffix]&#8221;<\/b>:<\/p>\n<pre>sed -i\".bak\" '3d' filename.txt<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to remove one or more lines from a file. Syntax:\u00a0 sed &#8216;{[\/]&lt;n&gt;|&lt;string&gt;|&lt;regex&gt;[\/]}d&#8217; &lt;fileName&gt; sed &#8216;{[\/]&lt;adr1&gt;[,&lt;adr2&gt;][\/]d&#8217; &lt;fileName&gt; \/&#8230;\/=delimiters n\u00a0= line [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1678","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1678"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1678\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}