ChatGPT解决这个技术问题 Extra ChatGPT

Regex: Remove lines containing "help", etc

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?


P
Peter Mortensen

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.

All lines containing the search term are bookmarked.

Now go to the menu Search → Bookmark → Remove Bookmarked lines

Done.


Yeah! It also has 'Remove Unmarked Lines', which is like grep, exactly what I needed!
How to delete/copy lets say 3 consecutive following lines after a bookmarked line for all bookmarks?
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:)
checking BOOKMARK LINE is important here otherwise it won't work!.
It is not work for me, bookmarked line do not deleting
O
OozeMeister

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.

https://i.stack.imgur.com/P3i0T.png


better answer than using mark tab, since this can be done for "Find in Files"
Make sure to escape special characters like parentheses with a backslash.
make sure to uncheck the ". matches newline"
Better answer due to a higher performance. With files of >1millions lines the bookmark method is stretched to its maximum, also on modern machines.
This causes the next line to sometimes get indented by TAB. Why?
s
sidyll

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

grep calls the grep program, obviously -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. help is the pattern to match filename is the name of the input file > redirects the output to the following item 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.


@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 it knew what grep was and it did a bunch of output, it could have been because I didn't specify an output file
@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
grep > sed any day of the week for FINDING sequences of characters
P
Peter Mortensen

Search with a regular expression:

^.*(help).*$

What about the deletion part?
@PeterMortensen Find \n\n, replace with `` (nothing)
In Windows, however, one can do \r\n\r\n and replace it with nothing.
T
Tikhon Jelvis

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


@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.
Thanks @TikhonJelvis ! And one final question, can we do sed 'help/' to delete all the lines starting (not containing) with help?
@CengizFrostclaw: I think sed '/^help/ d' should work. The ^ represents the start of the line.
@jeff PowerShell has sed and grep
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 babun.github.io. It acts as a wrapper around cygwin, but provides lots of stuff out of the box.
A
Alex from Jitbit

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.


"If you're on Windows," wait, what? I thought Notepad++ was only for Windows?
@FaranAiki maybe, I don't keep track what OS Notepad++ is for.