{"id":8728,"date":"2019-04-19T07:59:10","date_gmt":"2019-04-19T07:59:10","guid":{"rendered":"http:\/\/198.58.113.91\/blog\/?p=8728"},"modified":"2019-08-13T13:01:09","modified_gmt":"2019-08-13T13:01:09","slug":"remove-lines-containing-word-string-text-file","status":"publish","type":"post","link":"https:\/\/www.winhelponline.com\/blog\/remove-lines-containing-word-string-text-file\/","title":{"rendered":"How to Remove Lines Containing a Word or String in a Text File"},"content":{"rendered":"<p>If you have hundreds of lines in a text file and want to bulk delete lines that contain a word or string, this article is for you.<\/p>\n<p>Let&#8217;s use the excellent third-party text editor <a href=\"https:\/\/www.winhelponline.com\/blog\/replace-notepad-text-editor-notepad-plus-association\/\">Notepad++<\/a> (free) for deleting lines containing a word in a text-based file, using different methods.<!--more--><\/p>\n<h2>Remove Lines Containing a Word, Phrase or String in a Text File<\/h2>\n<p><em><strong>Scenario:<\/strong> I have a huge <code>HOSTS<\/code> file containing thousands of lines in it. I want to remove MSN advertising server entries from the file. In other words, I need to remove lines containing the string &#8220;<strong>.msn.com<\/strong>&#8221; in the <code>HOSTS<\/code> file. Let&#8217;s use Notepad++ for the job.<\/em><\/p>\n<p><em>Related: <a href=\"https:\/\/www.winhelponline.com\/blog\/replace-notepad-text-editor-notepad-plus-association\/\">How to Replace Notepad With Notepad++ or Any Other Text Editor?<\/a><\/em><\/p>\n<div class=\"qt\"><em><strong>Note:<\/strong> As always, be sure to create a backup copy the text file before modifying it.<\/em><\/div>\n<h3>Method 1: Remove lines using Bookmark feature in Notepad++<\/h3>\n<ol>\n<li>Open the text-based file using Notepad++<\/li>\n<li>Press <kbd>Ctrl<\/kbd> + <kbd>F<\/kbd> to open the Find and Replace dialog.<\/li>\n<li>Click to select the <strong>Mark<\/strong> tab.<\/li>\n<li>Type the search word or phrase in the &#8220;Find what&#8221; text box. In this example, I&#8217;d be typing <strong><code>.msn.com<\/code><\/strong><\/li>\n<li>Enable the <strong>Bookmark line<\/strong> checkbox.<\/li>\n<li>Set <strong>Search Mode<\/strong> to Normal.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8734\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-1.png\" alt=\"remove lines containing a word or string in a text file\" width=\"848\" height=\"571\" srcset=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-1.png 848w, https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-1-768x517.png 768w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/li>\n<li>Click <strong>Mark All<\/strong>.This marks (bookmarks) all the lines containing the string <code>.msn.com<\/code>, as seen below:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8732\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-not-containing-word-3.png\" alt=\"remove lines containing a word or string in a text file\" width=\"701\" height=\"541\" \/><\/li>\n<li>Close the Find dialog by clicking Close<\/li>\n<li>From the Search menu, click Bookmark, and click <strong>Remove Bookmarked Lines<\/strong>.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8731\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-3.png\" alt=\"remove lines containing a word or string in a text file\" width=\"770\" height=\"258\" srcset=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-3.png 770w, https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-3-768x257.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/li>\n<\/ol>\n<p>This removes all lines that contain the search string\/word\/phrase in the text file.<\/p>\n<div class=\"qt\">\n<h3>How to Remove lines that DO NOT contain a word or string?<\/h3>\n<p>To do the opposite of the above &#8212; i.e., delete lines that do not contain a word or phrase:<\/p>\n<ol>\n<li>Repeat the steps 1 \u2192 8 above.<\/li>\n<li>In step 9, from the Search menu in Notepad++, click <strong>Remove Unmarked Lines<\/strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9596\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/remove-unmarked-lines-notepad-plus.png\" alt=\"remove lines containing a word notepad++\" width=\"703\" height=\"246\" \/><\/li>\n<\/ol>\n<p>This removes all the lines except the ones which contain the search word or string.<\/p>\n<\/div>\n<h3>Method 2: Delete lines using Find and Replace method with RegEx<\/h3>\n<p>This method uses regular expressions to find and replace lines containing a word or phrase. This method is very powerful as you can match almost anything (such as words &#8220;beginning with&#8221;, or lines that have a specific &#8220;pattern&#8221;.)<\/p>\n<ol>\n<li>Open the text-based file using Notepad++.<\/li>\n<li>Press <kbd>Ctrl<\/kbd> + <kbd>F<\/kbd> to open the Find and Replace dialog.<\/li>\n<li>Click the <strong>Replace<\/strong> tab to select it.<\/li>\n<li>In the <strong>Find what:<\/strong> text box, type the search word, preceded and followed by <code>.*<\/code><em>e.g., if you&#8217;re wanting to replace lines containing the word <code>books<\/code>, you&#8217;d type <code>.*books.*<\/code><\/em>\n<p>Whereas, it&#8217;s <strong>slightly different in our case<\/strong> where we have two (special) <code>.<\/code> (dot) characters in our search string <code>.msn.com<\/code>. So we need to type the following in the <strong>Find what:<\/strong> text box:<\/p>\n<pre>.*\\.msn\\.com.*<\/pre>\n<div class=\"qt\"><code>.*<\/code> &#8211; matches any character any number of repetitions.<br \/>\n<code>\\.<\/code> &#8211; is used to escape the dot (.)So, the system understands you&#8217;re looking to match the string <strong><code>.msn.com<\/code><\/strong><\/p>\n<\/div>\n<\/li>\n<li>Set the Search Mode to <strong>Regular expression<\/strong><\/li>\n<li>Make sure that the <strong>Replace with:<\/strong> text box is left blank.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8735\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/replace-line-containing-word-regex.png\" alt=\"remove lines containing a word or string in a text file using regex\" width=\"573\" height=\"356\" \/><\/li>\n<li>Click <strong>Replace All<\/strong>.<em>Now, Notepad++ replaces all those matching lines with blank lines. In the Replace dialog, you&#8217;ll see the number of occurrences replaced. Next, you need to remove those blank lines.<\/em><\/li>\n<li>Close the Find\/Replace dialog.<\/li>\n<li>To remove the empty lines, click <strong>Edit<\/strong> \u2192 Line Operations \u2192 <strong>Remove Empty Lines<\/strong>.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8729\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/remove-empty-lines-notepad-plus.png\" alt=\"remove lines containing a word or string in a text file\" width=\"692\" height=\"512\" \/><\/li>\n<\/ol>\n<p>This removes all the lines except the ones which contain the search word or string.<\/p>\n<div class=\"qt\">\n<h3>How to Remove lines that DO NOT contain a word or string using Regex?<\/h3>\n<p>To do the opposite of the above &#8212; i.e., delete lines that do not contain a word or phrase using Regex:<\/p>\n<ol>\n<li>Follow steps 1 to 3 above.<\/li>\n<li>In step 4, use the regex search keyword <code>^(?!.*\\.msn\\.com).*$<\/code><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9597\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/remove-lines-not-containing-word-regex-notepad-plus.png\" alt=\"remove lines not containing a word notepad++ regex\" width=\"573\" height=\"356\" \/>\n<p>The above search operator finds lines that don&#8217;t contain the word or string <code>.msn.com<\/code> and replaces them with empty lines.<\/li>\n<li>Then follow steps 5 to 9 to remove the empty lines.<\/li>\n<\/ol>\n<\/div>\n<p>That&#8217;s it! Hope the above methods proved helpful to quickly remove lines containing (or not containing) a specific string, word or phrase in a text file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have hundreds of lines in a text file and want to bulk delete lines that contain a word or string, this article is for you. Let&#8217;s use the excellent third-party text editor Notepad++ (free) for deleting lines containing a word in a text-based file, using different methods.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,7],"tags":[339],"class_list":["post-8728","post","type-post","status-publish","format-standard","hentry","category-utilities","category-windows","tag-notepad"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":9886,"url":"https:\/\/www.winhelponline.com\/blog\/notepad-plus-find-and-replace-text\/","url_meta":{"origin":8728,"position":0},"title":"Notepad++ Tips: Find and Replace, and Text Manipulation Examples","author":"Ramesh","date":"June 21, 2019","format":false,"excerpt":"Notepad++ is an excellent light-weight text editor with many useful features. With Notepad++, you can find and replace text in the current file or in multiple files in a folder recursively. You can also find and replace text using regex. This post has many Notepad++ find & replace examples and\u2026","rel":"","context":"In &quot;Utilities&quot;","block_context":{"text":"Utilities","link":"https:\/\/www.winhelponline.com\/blog\/category\/utilities\/"},"img":{"alt_text":"notepad++ tips - remove duplicate rows without sorting","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/06\/notepad-plus-remove-duplicate-rows-without-sort.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/06\/notepad-plus-remove-duplicate-rows-without-sort.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/06\/notepad-plus-remove-duplicate-rows-without-sort.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":10337,"url":"https:\/\/www.winhelponline.com\/blog\/flip-or-reverse-text-file-contents-windows\/","url_meta":{"origin":8728,"position":1},"title":"How to Flip or Reverse a Text File Using Different Ways in Windows","author":"Ramesh","date":"July 14, 2019","format":false,"excerpt":"When analyzing plain-text log files such as the Web server logs or the Sfc, or DISM log files, you may sometimes prefer the most recent items appearing at the top. However, most of the log files that Windows generates would have the oldest entries listed first -- entries listed in\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"reverse lines in a text file notepad++","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/07\/reverse-text-file-notepad-plus-5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/07\/reverse-text-file-notepad-plus-5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/07\/reverse-text-file-notepad-plus-5.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4426,"url":"https:\/\/www.winhelponline.com\/blog\/find-amount-words-lines-chars-text-file-powershell\/","url_meta":{"origin":8728,"position":2},"title":"Find the Amount of Words, Chars and Lines in a Text File Using PowerShell","author":"Ramesh","date":"October 23, 2016","format":false,"excerpt":"If you're still using the good, old Notepad to craft essays or content for your Website, and want to know the number of characters used in a text file, here is a PowerShell cmdlet that will help.PowerShell has a built-in cmdlet named \"Measure-Object\". It calculates the property values of certain\u2026","rel":"","context":"In &quot;Windows 10&quot;","block_context":{"text":"Windows 10","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-10\/"},"img":{"alt_text":"get word count in text file","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/10\/get-word-count-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/10\/get-word-count-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/10\/get-word-count-1.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":213,"url":"https:\/\/www.winhelponline.com\/blog\/add-the-open-with-notepad-context-menu-option-for-all-file-types\/","url_meta":{"origin":8728,"position":3},"title":"Add &#8220;Open with Notepad&#8221; to the Right-click Menu for all file types","author":"Ramesh","date":"April 24, 2008","format":false,"excerpt":"There are situations where you need to open plain-text files having an unknown or unregistered file extension using Notepad. By default, no file association exists for extension-less files and files with unknown extension in Windows. So, to open those files, you use the Open With option and select Notepad from\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"add open with notepad to right-click","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2008\/04\/open_with_notepad.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2008\/04\/open_with_notepad.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2008\/04\/open_with_notepad.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":5732,"url":"https:\/\/www.winhelponline.com\/blog\/replace-notepad-text-editor-notepad-plus-association\/","url_meta":{"origin":8728,"position":4},"title":"How to Replace Notepad With Notepad++ or Other Editor","author":"Ramesh","date":"November 2, 2017","format":false,"excerpt":"If you\u2019re looking at how to replace Notepad with Notepad++ or any third-party editor, this post explains how to do it. Notepad++ is a free source code editor and Notepad replacement that supports several languages. Let\u2019s see how you can replace Notepad with Notepad++ without replacing any system files. Notepad\u2026","rel":"","context":"In &quot;Utilities&quot;","block_context":{"text":"Utilities","link":"https:\/\/www.winhelponline.com\/blog\/category\/utilities\/"},"img":{"alt_text":"notepad plus replace debugger notepad.exe","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/debugger-notepad-plus-switch.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/debugger-notepad-plus-switch.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/debugger-notepad-plus-switch.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/debugger-notepad-plus-switch.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":35642,"url":"https:\/\/www.winhelponline.com\/blog\/new-text-document-missing-windows-11\/","url_meta":{"origin":8728,"position":5},"title":"New &#8220;Text Document&#8221; missing from Right-Click menu in Windows 11","author":"Ramesh","date":"April 3, 2023","format":false,"excerpt":"When you right-click on the desktop and click \"New,\" the New menu may not contain the \"Text Document\" option on your Windows 11 computer. This article tells you how to bring the \"Text Document\" option back. Restore Missing \"Text Document\" to the New menu To add \"Text Document\" to the\u2026","rel":"","context":"In &quot;Windows 11&quot;","block_context":{"text":"Windows 11","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-11\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/8728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/comments?post=8728"}],"version-history":[{"count":0,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/8728\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/media?parent=8728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/categories?post=8728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/tags?post=8728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}