Delete line if does not start with a specified character or word

Help with writing and playing macros

Delete line if does not start with a specified character or word

Postby Javier » Fri Sep 09, 2005 9:44 am

I need to delete all lines in a text file that doesn't start with "@".

For example

FFFFFF
@GGG
AAAAA
@BBBB

should result:

@GGG
@BBBB

i'm trying with IfnotFound but I'm getting errors.
any help? thanks
User avatar
Javier
Newbie
 
Posts: 2
Joined: Thu Sep 08, 2005 11:00 pm

Re: Delete line if does not start with a specified character or word

Postby mrainey56 » Fri Sep 09, 2005 10:47 am

Do a regular Search/Replace All
Regular Expressions checked (Unix-style)

Find What: ^[^@].*\p
Replace With: nothing
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Delete line if does not start with a specified character or word

Postby Javier » Fri Sep 09, 2005 11:00 am

I've edited the next macro but it's not working

InsertMode
ColumnModeOff
HexOff
UnixReOn
Top
Find RegExp "^[^@].*\p"
Replace All ""

Is this correct?

Edit: RegExp parameter was missing.
User avatar
Javier
Newbie
 
Posts: 2
Joined: Thu Sep 08, 2005 11:00 pm

Re: Delete line if does not start with a specified character or word

Postby Doug E » Mon Nov 17, 2008 3:51 am

Using this thread to modify my own macros I've come up with the problem..

When I search for ^[^@].*\p in a file containing the lines...

Code: Select all
FFFFFF
@GGG
AAAAA
@BBBB

it works properly showing me the lines...

Code: Select all
FFFFFF
AAAAA

But when I use it for my own purposes I try searching for ^[^http].*\p in the lines....

Code: Select all
http://www.www.com/
http://www.www.com
ftp.www.com
http://www.ftp.com

And I get an UltraEdit popup saying...

Code: Select all
Search string '^[^http].*\p' not found!

I am sure to check Unix Regular Expressions so what fundamental gap in knowledge am I missing here?
Doug E
Newbie
 
Posts: 5
Joined: Fri Nov 07, 2008 8:09 pm

Re: Delete line if does not start with a specified character or word

Postby pietzcker » Mon Nov 17, 2008 7:47 am

[^http] means "Match any one character except h, t or p". So your regex should match ftp.www.com (and it does for me in v14.20.0.1035). However, it will also match a line like "this line starts with a t".

What you want to do requires Perl regular expressions.

^\s*(?!http).*\r\n

will match a (DOS file) line that doesn't start with "http" (leading whitespace ignored).
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Delete line if does not start with a specified character or word

Postby Doug E » Mon Nov 17, 2008 10:49 am

Ahh, beautiful. Thanks pietzcker!

I spent much of the night reading how to do it with unix expressions. I'll have to read up on perl next.

Some keywords to help people find it: find word at start of the line cut it select line out paste it into new file txt

And the description of what it does...

Searches for lines that don't start with desired words (http in this case), selects those lines, cuts them out and pastes them into a newly opened file so you can review what was removed from the original file.

Show Cancel dialog for this macro: unchecked
Continue if search string not found: checked

InsertMode
ColumnModeOff
HexOff
PerlReOn
Top
Clipboard 9
ClearClipboard
Loop 0
Find RegExp "^\s*(?!http).*\r\n"
IfFound
CutAppend
Else
ExitLoop
EndIf
EndLoop
NewFile
Paste
ClearClipboard
Clipboard 0
Top
Doug E
Newbie
 
Posts: 5
Joined: Fri Nov 07, 2008 8:09 pm


Return to Macros