Regex: Remove lines containing "help", etc

RegexNotepad++

Regex Problem Overview


I have a long document of commands. Using Notepad++ or regex, I want to delete all lines containing "help" including keyboard_help, etc.

How can this be done?

Regex Solutions


Solution 1 - Regex

This is also possible with Notepad++:

  • Go to the search menu, Ctrl + F, and open the Mark tab.

  • Check Bookmark line (if there is no Mark tab update to the current version).

  • Enter your search term and click Mark All

    • All lines containing the search term are bookmarked.
  • Now go to the menu SearchBookmarkRemove Bookmarked lines

  • Done.

Solution 2 - Regex

Another way to do this in Notepad++ is all in the Find/Replace dialog and with regex:

  • Ctrl + h to bring up the find replace dialog.

  • In the Find what: text box include your regex: .*help.*\r?\n (where the \r is optional in case the file doesn't have Windows line endings).

  • Leave the Replace with: text box empty.

  • Make sure the Regular expression radio button in the Search Mode area is selected. Then click Replace All and voila! All lines containing your search term help have been removed.

How-To Line Replace in N++

Solution 3 - Regex

Easy task with grep:

grep -v help filename

Append > newFileName to redirect output to a new file.


Update

To clarify it, the normal behavior will be printing the lines on screen. To pipe it to a file, the > can be used. Thus, in this command:

grep -v help filename > newFileName
  1. grep calls the grep program, obviously
  2. -v is a flag to inverse the output. By defaulf, grep prints the lines that match the given pattern. With this flag, it will print the lines that don't match the pattern.
  3. help is the pattern to match
  4. filename is the name of the input file
  5. > redirects the output to the following item
  6. newFileName the new file where output will be saved.

As you may noticed, you will not be deleting things in your file. grep will read it and another file will be saved, modified accordingly.

Solution 4 - Regex

Search with a regular expression:

^.*(help).*$

Solution 5 - Regex

You can do this using sed: sed '/help/ d' < inputFile > outputFile

Solution 6 - Regex

If you're on Windows, try findstr. Third-party tools are not needed:

findstr /V /L "searchstring" inputfile.txt > outputfile.txt

It supports regex's too! Just read the tool's help findstr /?.

P.S. If you want to work with big, huge files (like 400 MB log files) a text editor is not very memory-efficient, so, as someone already pointed out, command-line tools are the way to go. But there's no grep on Windows, so...

I just ran this on a 1 GB log file, and it literally took 3 seconds.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondukevinView Question on Stackoverflow
Solution 1 - RegexstemaView Answer on Stackoverflow
Solution 2 - RegexOozeMeisterView Answer on Stackoverflow
Solution 3 - RegexsidyllView Answer on Stackoverflow
Solution 4 - Regexuser8998146View Answer on Stackoverflow
Solution 5 - RegexTikhon JelvisView Answer on Stackoverflow
Solution 6 - RegexAlex from JitbitView Answer on Stackoverflow