475

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dukevin
  • 22,384
  • 36
  • 82
  • 111

7 Answers7

1169

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stema
  • 90,351
  • 20
  • 107
  • 135
  • 46
    Yeah! It also has 'Remove Unmarked Lines', which is like grep, exactly what I needed! – Chloe May 18 '12 at 16:45
  • How to delete/copy lets say 3 consecutive following lines after a bookmarked line for all bookmarks? – Joarder Kamal May 15 '14 at 01:44
  • This approach works, but if the file is big (100MB) and many items are bookmarked it becomes very slow to mark them and especially "copy bookmarked items". Grep is much faster(better:) – shtolik Oct 02 '14 at 13:53
  • 10
    checking `BOOKMARK LINE` is important here otherwise it won't work!. – Haseeb Mir Mar 13 '18 at 21:40
  • This works but is a long winded process if you have to repeatedly remove multiple different matches – bytedev Sep 25 '18 at 11:01
  • 3
    It is not work for me, bookmarked line do not deleting – Dmitrij Holkin May 25 '20 at 06:47
  • 2
    Instead of ```Ctrl + F``` and clicking "Mark" tab, you can directly press ```Ctrl + M``` – Makesh Jun 23 '21 at 12:37
  • "Remove Bookmarked Lines" did not work for me either, until I realized that I actually did not bookmark lines by clicking "Mark all". The "Boomark Line" checkbox needs to be checked as well. – Anders Lindén Mar 18 '22 at 13:19
  • @bytedev You can add multiple different search words separated by `|`. Example search words `red|green|blue`. But to get this syntax work, you have to choose "Regular Expression" as Search mode – PeterCo Jul 09 '23 at 14:15
239

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++

OozeMeister
  • 4,638
  • 1
  • 23
  • 31
21

Search with a regular expression:

^.*(help).*$
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
19

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.

sidyll
  • 57,726
  • 14
  • 108
  • 151
  • @Kevin Duke: Alas! That probably means you don't have `grep` installed. Anyway I'd recommend you installing GNU grep, it will certainly work on Windows, and it's a really useful tool. – sidyll May 03 '11 at 22:29
  • @sidyll it knew what grep was and it did a bunch of output, it could have been because I didn't specify an output file – dukevin May 03 '11 at 22:48
  • 2
    @Kevin Duke: as I said earlier in the answer, the output can be redirected. The standard behavior is print on the screen. To redirect, you use a pipe (`>` in this case), giving a final command of `grep -v help filename > outputFileName` – sidyll May 03 '11 at 22:50
  • grep > sed any day of the week for FINDING sequences of characters – kwikness Oct 30 '14 at 20:14
14

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

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • 4
    @CengizFrostclaw: At the command line on Linux/Mac/Whatever. If you're on Windows, you'd have to install something like CygWin and use that. – Tikhon Jelvis Mar 20 '14 at 23:11
  • Thanks @TikhonJelvis ! And one final question, can we do sed 'help/' to delete all the lines **starting** (not containing) with help? – jeff Mar 21 '14 at 20:15
  • 3
    @CengizFrostclaw: I think `sed '/^help/ d'` should work. The `^` represents the start of the line. – Tikhon Jelvis Mar 22 '14 at 01:56
  • @jeff PowerShell has `sed` and `grep` – Ooker Oct 31 '17 at 15:24
  • cygwin may not work on windows with utf-16 files. Cygwin utilities use UFT-8 by default. Very few of them support UTF-16. Use babun http://babun.github.io/. It acts as a wrapper around cygwin, but provides lots of stuff out of the box. – Sahil Singh Jan 24 '19 at 11:16
10

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.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
1

You have to do it in two steps.

1. Bookmark the lines having that pattern

Press CTRL + M button. In the find what text box, enter the pattern. Then select the check box labeled as Bookmark Line. Then click the button named "Mark All". Close the dialog box.

2. Delete those bookmarked lines

Press ALT + S(earch) + B(ookmark) + R. Press Enter.

You are done.

Aditya Bhuyan
  • 328
  • 6
  • 10