{"id":2529,"date":"2019-04-03T22:11:27","date_gmt":"2019-04-03T19:11:27","guid":{"rendered":"http:\/\/kifarunix.com\/?p=2529"},"modified":"2020-02-23T15:44:29","modified_gmt":"2020-02-23T12:44:29","slug":"delete-lines-matching-specific-pattern-in-a-file-using-vim","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/delete-lines-matching-specific-pattern-in-a-file-using-vim\/","title":{"rendered":"Delete Lines Matching Specific Pattern in a File using VIM"},"content":{"rendered":"<p>This guide will take you through how to delete lines matching specific pattern in a file using VIM. Linux system admins work with files day in day out. At some point, you may want just to delete lines matching a specific pattern or a string in a file. There are quite a number of tools out there that you can use to achieve the same. However, this we are going to focus on using VIM.<\/p>\n<h3>Delete Lines Matching Specific Pattern in a File Using VIM<\/h3>\n<p>In order to delete lines matching a pattern in a file using <code>vim<\/code> editor, you can use <code>ex<\/code> command, <code>g<\/code> in combination with <code>d<\/code> command.<\/p>\n<p>To remove lines that contains the string <code>amos<\/code>, in vim command mode, type the command below and press Enter.<\/p>\n<pre>:g\/amos\/d<\/code><\/pre>\n<p>Similarly, to delete multiple lines with multiple patterns, you can use;<\/p>\n<pre>:g\/amos\\|mibey\/d<\/code><\/pre>\n<p>This will delete all lines containing the specified keywords.<\/p>\n<p>To delete empty lines or lines with white spaces (<code>s<\/code>);<\/p>\n<pre>:g\/^\\s*$\/d\nor\n:g\/^$\/d<\/code><\/pre>\n<p>To delete all the lines that do not contain a specific pattern. For example to delete all the lines that do not contain the string <code>amos<\/code><\/p>\n<pre>:g!\/amos\/d<\/code><\/pre>\n<p>To delete all the lines that do not contain more than one string for example that do not contain the strings <code>amos<\/code> or <code>mibey<\/code>.<\/p>\n<pre>:g!\/amos\\|mibey\/d<\/code><\/pre>\n<p>You can also use <code>v<\/code> instead of <code>g!<\/code>.<\/p>\n<pre>:v\/amos\\|mibey\/d<\/code><\/pre>\n<p>That is all about how to delete lines matching specific pattern in a file using VIM. If you have other suggestions, be sure to drop a comment. Thank you.<\/p>\n<p>Are you looking at deleting lines matching specific patterns or keyword in a file using SED? See our article by following the link below;<\/p>\n<p><a href=\"https:\/\/kifarunix.com\/delete-lines-matching-a-specific-pattern-in-a-file-using-sed\/\" target=\"_blank\" rel=\"noopener noreferrer\">Delete Lines Matching a Specific Pattern in a File using SED<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide will take you through how to delete lines matching specific pattern in a file using VIM. Linux system admins work with files day<\/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":[121,49,374],"tags":[1283,1284,1285,375],"class_list":["post-2529","post","type-post","status-publish","format-standard","hentry","category-howtos","category-command-cheatsheets","category-vim","tag-delete-lines-matching-a-pattern-in-vim","tag-delete-multiple-lines-with-a-pattern-vim","tag-delete-vim","tag-vim","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2529"}],"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=2529"}],"version-history":[{"count":3,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2529\/revisions"}],"predecessor-version":[{"id":5033,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2529\/revisions\/5033"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}