Find and delete
I’ve looked through the FAQ’s and Regex docs. And I’ve examined Peter Jones Find/Replace and Delete Spaces responses a few days ago, but apparently I’m not clever enough to “get it”.
I just need to find a specific text string in a text doc and delete it, including the LF character, I guess. I have a log file with a bunch of repeating errors I want to delete from the file, like this:
Invalid character # 124
I can search for those, and leave the Replace entry blank, and all those strings are removed, but their LF characters remain, leaving the log file with just as many lines as before. I’d like to make those lines go away completely.
I guess I just need to know what to enter in the Replace area to facilitate that.
Usually to remove a line the regex will also include the
\Rmetacharacter. This refers to the carriage return/line feed (Windows), or depending on which OS is involved it may just be a carriage return or just a line feed.
So if your line is JUST
Invalid character # 124and then an end of line marker, then the regex find will be
Invalid character # 124\R
assuming you are using the search mode ‘regular expression’.
You ONLY use the replace field if you want to put something back, either consisting of the text you found (possibly altered) or a replacement text. In your case I’d suggest you DON’T want to use the replace field as you are ONLY removing text/line(s).
As always we have to do a lot of interpretation of what the request is. Please read the FAQ section of the forum, especially the one titled
FAQ Desk: Request for Help without sufficient information to help you
You can help us to help you by providing GOOD definitive examples, before and after results and any other situations where maybe you DON’T want to remove some text/line(s).
I should also say there is more than 1 way to skin a cat. By that I mean, if you want an (easier?) method of removing a line containing the text but possibly the line has other characters which may change then the alternate method is to use the “Mark” function with the “bookmark” option ticked.
When in the Replace function (which i assume you already know about since you mention it), look at the tabs just above, you will see ‘Find’, ‘Find in Files’ and also ‘Mark’. Select ‘Mark’, type in the static text and tick the ‘bookmark’ option. Now if using just static text your search mode can be ‘normal’. Click on ‘Mark All’. What will happen is ALL lines containing the text will have a marker at the start of the line. You could look through the file to confirm all markers are correct (generally a good idea the first few times until sure the text you enter does not inadvertently select something you don’t want removed).
Once confirmed use the main menu option ‘Search’, then ‘Bookmark’ (generally at bottom of dropdown menu), then ‘Remove Bookmarked Lines’. Job done!
Thank you, thank you!
Both methods worked as described.
I had found the \R in the regex docs, but what I didn’t do was set the Search mode from Normal to Regular expression, so thanks for mentioning that!
And the Mark mode works with \R in Regular expressions mode, or without the \R in Normal mode. So it’s a win win.
Glad it was easily solved. You are starting to see the power of ‘regex’, over a ‘normal’ search mode. Normal will only allow for static information, whereas the ‘regular expression’ mode is essentially a formula created in such a way as to seek out those character patterns (even if changing). In ‘normal’ mode the
\is just a backslash and
Ris just a capital R. BUT, when you combine them under ‘regular expression’ they become a metacharacter, basically a ‘bit of code’ to mean something special to the regex engine. Under ‘regular expression’ mode we’d need to have
\\Rif we just wanted a backslash followed by a capital R. The first
\identifies what follows as being special (only certain characters can be special), however in this case because the second
\is already special it allows for it to become normal again.
Good luck and be sure to ‘seek’ out some of those supporting websites (and books) to learn more about “The Power of Regex” if you want to get better at it.
I was where you are now about 8 months ago, I’m still learning. There are many helpful ‘citizens’ on this forum who will also help. All we ask for is that you (the OP) give us enough information to help us to help you, and also we hope it will lead you onto learning more.