{"id":2531,"date":"2019-04-10T22:08:59","date_gmt":"2019-04-10T19:08:59","guid":{"rendered":"http:\/\/kifarunix.com\/?p=2531"},"modified":"2019-04-20T10:16:36","modified_gmt":"2019-04-20T07:16:36","slug":"delete-lines-matching-a-specific-pattern-in-a-file-using-sed","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/delete-lines-matching-a-specific-pattern-in-a-file-using-sed\/","title":{"rendered":"Delete Lines Matching a Specific Pattern in a File using SED"},"content":{"rendered":"<p>In this guide, we are going to learn how to delete lines matching a specific pattern in a file using SED. SED is a stream editor that performs basic text filtering and transformations on an input stream (a file or input from a pipeline).<\/p>\n<h3>Delete Lines Matching a Specific Pattern in a File using SED<\/h3>\n<p>In our previous guide, we covered how to delete lines matching specific patterns in VIM. You can check by following the link below;<\/p>\n<ul>\n<li class=\"entry-title td-module-title\"><a title=\"Delete Lines Matching Specific Pattern in a File using VIM\" href=\"https:\/\/kifarunix.com\/delete-lines-matching-specific-pattern-in-a-file-using-vim\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Delete Lines Matching Specific Pattern in a File using VIM<\/a><\/li>\n<\/ul>\n<p>Now, let us go through various examples of deleting lines matching specific pattern in a file using SED. For the purposes of demonstration, we will be using the seven colors of rainbow in a file.<\/p>\n<pre><strong>cat colors<\/strong>           \nThis is color red\nThis is color orange\nThis is color yellow\nThis is color green\nThis is color blue\nThis is color indigo\nThis is color violet<\/code><\/pre>\n<p>Just like in VIM, we will be using the <code>d<\/code> command to delete specific pattern space with SED. To begin with, if you want to delete a line containing the keyword, you would run sed as shown below.<\/p>\n<pre>sed -i '\/pattern\/d' file<\/code><\/pre>\n<p>Where option <code>-i<\/code> specifies the file in place. If you need to perform a <strong>dry run<\/strong> (without actually deleting the line with the keyword) and print the result to std output, omit option <code>-i<\/code>. For example, do delete a line containing the keyword <strong>green<\/strong>, you would run;<\/p>\n<pre><strong>sed '\/green\/d' colors\n<\/strong>This is color red\nThis is color orange\nThis is color yellow\nThis is color blue\nThis is color indigo\nThis is color violet<\/code><\/pre>\n<p>Similarly, you could run the sed command with option <code>-n<\/code> and negated <code>p<\/code>,<code>(!p)<\/code> command.<\/p>\n<pre><strong>sed -n '\/green\/!p' colors<\/strong>           \nThis is color red\nThis is color orange\nThis is color yellow\nThis is color blue\nThis is color indigo\nThis is color violet<\/code><\/pre>\n<p>To delete lines containing multiple keywords, for example to delete lines with the keyword <strong>green<\/strong> or lines with keyword <strong>violet<\/strong>.<\/p>\n<pre><strong>sed '\/green\\|violet\/d' colors<\/strong>\nThis is color red\nThis is color orange\nThis is color yellow\nThis is color blue\nThis is color indigo<\/code><\/pre>\n<p>If you need to delete the lines but create a backup of the original file, then use option -i in the format, <code>-i.bak<\/code>. This will create a <code>.bak<\/code> of the original file name.<\/p>\n<pre>sed -i.bak '\/green\\|violet\/d' colors<\/code><\/pre>\n<p>To delete all the lines except that contain a specific pattern or keyword;<\/p>\n<pre>sed '\/green\/!d' colors\nThis is color green<\/code><\/pre>\n<pre>sed '\/green\\|violet\/!d' colors \nThis is color green\nThis is color violet<\/code><\/pre>\n<p>To delete lines starting with a specific character or pattern, for example, the comment lines starting with # or lines begining with keyword amos.<\/p>\n<pre>sed '\/^#\/d' colors<\/code><\/pre>\n<pre>sed '\/^amos\/d' colors<\/code><\/pre>\n<p>To delete lines ending with a specific pattern or a character, for exmple to delete lines starting ending with character o or keyword amos;<\/p>\n<pre>sed '\/o$\/d' colors<\/code><\/pre>\n<pre>sed '\/amos$\/d' colors<\/code><\/pre>\n<p>To combine the above such that you can delete lines ending with letter o or keyword amos;<\/p>\n<pre>sed '\/o$\\|amos$\/d' colors<\/code><\/pre>\n<p>To delete blank lines<\/p>\n<pre>sed '\/^$\/d' colors<\/code><\/pre>\n<p>To delete blank lines or lines that contain one or more white spaces;<\/p>\n<pre>sed '\/^ *$\/d' colors<\/code><\/pre>\n<p>To delete blank lines or lines that one or more tabs;<\/p>\n<pre>sed '\/^\\t*$\/d' colors<\/code><\/pre>\n<p>Well, this is just the little we could cover about how to delete lines matching a specific pattern in a file using SED. Feel free to add more examples and suggestions in the comments below. Enjoy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to delete lines matching a specific pattern in a file using SED. SED is a stream<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[49,383],"tags":[384],"class_list":["post-2531","post","type-post","status-publish","format-standard","hentry","category-command-cheatsheets","category-sed","tag-sed","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2531"}],"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=2531"}],"version-history":[{"count":4,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2531\/revisions"}],"predecessor-version":[{"id":2574,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2531\/revisions\/2574"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=2531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=2531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=2531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}