find rows beginning with and containing at the same time

General questions about using TextPad

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

Post Reply
bensonuser
Posts: 10
Joined: Wed Mar 12, 2008 2:33 pm

find rows beginning with and containing at the same time

Post by bensonuser »

I am trying to find rows beginning with 158 and containing a -
The dash can be in different positions. Some rows may even contain more than one. The file contains rows that begin with codes other than 158 but do contain a dash. They should be ignored.
Once I find the row beginning with 158 and containing dashes, I want to delete the dashes.
I can get how to find the rows beginning with 158 but can't figure out the "and contains" part.

158AS1 00000300066.580
158AS5 00000400048.800
158AS59 00000200045.420
158AX3 00000100022.800
158CB-125U 00000900000.640
158CH-14XV 00000100007.860
158CH-303 00000300002.270
158CH-307 00000300002.930
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Find what: ^(158.*)-
Replace with: \1

[X] Regular expression

Replace All -- do this repeatedly until it beeps
This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
bensonuser
Posts: 10
Joined: Wed Mar 12, 2008 2:33 pm

Post by bensonuser »

thanks very much for your reply.
the replace command of \1 replaces the - with a blank so my end result looks like:

158CB 125U

i need it to look like this:

158CB125U

any suggestions would be appreciated.
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

ben_josephs' solution should be working for you. You may have copied a blank space after the \1 replacement. Make sure it is only replacing "\1" and not "\1 "

===============
Here is an alternative, longer, but does the same thing.
This will replace a single instance of a - anywhere on a line starting with 158.
You could do multiple passes to remove additional - characters.

Find what: (^158.*)-(.*)
Replace with: \1\2

Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------
Hope this was helpful.............good luck,
Bob
bensonuser
Posts: 10
Joined: Wed Mar 12, 2008 2:33 pm

solution to find and replace question

Post by bensonuser »

thanks for the quick and helpful responses.

this was my final solution.

i used find # (to make sure the symbol did not already exist in my data)
i used your find (^158.*)-
i changed the replace to: \1# (regular exp on and using replace all)
i repeated the replace all until the program responded with a cannot find
i then used:
find what: #
replace with nothing (regular expression off).
i recorded a macro and have tried it on a number of files and it is working nicely.
again, thanks for the direction.
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Why are you putting the #s in, only to take them out again?
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Did you do that because the original solution was still not working?

This may work, but makes no sense to me, and I want to keep learning from others. Why is it necessary to add/remove the #?
Hope this was helpful.............good luck,
Bob
bensonuser
Posts: 10
Joined: Wed Mar 12, 2008 2:33 pm

Post by bensonuser »

because i replaced the - with a # only on the lines that began with the codes i needed, i was then able to just use find and delete all once it had done the change.
i could not get the \1 not to leave a space. i am new to textpad so maybe i will figure it out eventually but i was running out of time so just used a quick way around it.
hey - it worked and that's what mattered.

thanks for pointing me in the right direction. it was your find logic that let me get going.
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Good luck.

As you said, it is working for you, and that is the most important issue.

But when you get a chance, make the time to go back and look at the single step RegEx solution(s), do an autopsy to understand what is happening, and you will soon be on your own.
Hope this was helpful.............good luck,
Bob
Post Reply