Delete a line based on content - Please Help :-)

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Delete a line based on content - Please Help :-)

Post by mmiller »

Hi Folks;

I need to trim down an 80 meg log file so what I need to do is delete every line that contains the text:

192.168.1.1

Can anyone give me a step by step on how to automate deleting all lines that contain 192.168.1.1 ?

Thanks!
Best Regards;
Marvin Miller
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

I've had a few glasses of stout so pardon any mistakes but you are looking to perform a search and replace. Your search criteria will be a regular expression. The expression you probably want to use is
.*192\.168\.1\.1.*

That ought to find any line that has 192.168.1.1 in it, be it at the begining, middle or end.

The replacement expression is blank, empty.

If this ends up leaving two blank lines, run an second search and replace for \n\n and replace it with \n. Someone more clever than I will post a better solution but those ought to work. Also, if they do work, make a macro and then you can play the solution in any of your log files to clean them up.
I choose to fight with a sack of angry cats.
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Post by mmiller »

Hi talleyrand;

Thanks for the reply! I tried copying and pasting the expression .*192\.168\.1\.1.* in TextPad but it says that it can't find it. I've verified that I have the right text file with the 192.168.1.1 lines in them :-)

All I did was copy and paste it into the search and replace box and then told it to do it. It can't find it so I suspect there might be something wrong with the expression? I don't know much about TextPad so you might have to forgive me if I messed it up somehow :-)
Best Regards;
Marvin Miller
bveldkamp

Post by bveldkamp »

Did you check Regular Expression in the Search/Replace dialog?

Alternatively you can do an ordinary search (Ctrl+F) on 192.168.1.1 (uncheck Regular Expression) but instead of Find Next, press Mark All, then Edit | Delete | Bookmarked lines.
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Post by mmiller »

Hi Berend;

Thanks - that's what it was. Once I checked off regular expression it worked like a top. Thanks guys - you made my life a lot easier :-)
Best Regards;
Marvin Miller
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

To do it in one go:
Find what: .*192\.168\.1\.1.*\n
Replace with: [nothing]

[X] Regular expression
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Post by mmiller »

I did find one issue though, in the file the lines with 192.168.1.1 are not all back to back. For instance, in most cases there's 50 lines in a row and then some other lines and then 3 lines in a row etc.

So after running the command to delete the lines my log files have various gaps in them. So in part there might be as much as 100 lines missing and in another there may be only 3 missing.

I did try and run the command to remove the blank lines but it only seemed to remove certain ones.

If there's an easy solution that would be great but at this point it's more cosmetic then anything. The key thing is trimming out the 192.168.1.1 lines and with everyone's help that's been accomplished :lol:
Best Regards;
Marvin Miller
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

If you follow my suggestion, the job is done in a single step. It deletes each line containing the IP address, including the terminating new line character. No empty lines are left.

If a file has sequences of empty lines, that is, sequences of new line characters, and you replace every pair of new lines with a single one, you halve the number in each sequence of new lines. So you have to perform the replacement repeatedly. You can't replace \n+ by \n; that is a deficiency of the regex engine used by TextPad.
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Post by mmiller »

Hi Ben;

My bad - I didn't notice that you had appended the \n at the end of your suggestion. I've been spending way too much time staring at the screen over the last few days so it's time for a break :-)

I'll do it that way on my next logfile. It was mentioned to create a mackerel to make this easier? What would that do and how would it work?
Best Regards;
Marvin Miller
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Mackerel is good! I like mackerel. Some may tell you to say macro, but take no notice. :-)

Here's how to record a fish that might do the trick :-) :

Macro | Record (<Ctrl+Shift+R>)
Search | Replace... (<F8>)
.*192\.168\.1\.1.*\n
<Tab>
<Del>
[Make sure that Regular expression is selected.]
Replace All
<Esc>
Macro | Stop Recording (Ctrl+Shift+R)
At this point you have a temporary fish; you can save it permanently if you wish.

You can replay the temporary fish with Macro | Playback (Ctrl+R).

You can save it permanently (unless you've overwritten it) with Macro | Save....

Look in the help under How to... | Use Keystroke Macros.
User avatar
mmiller
Posts: 17
Joined: Tue Nov 25, 2003 11:49 pm
Location: Canada
Contact:

Post by mmiller »

Holy Mackerel - I think it's gonna work :P

Thanks Ben - Have a great Christmas and a Happy New year :) You've saved me a lot of work and that's really good news :D
Best Regards;
Marvin Miller
Post Reply