Page 1 of 1
find rows beginning with and containing at the same time
Posted: Thu Mar 26, 2009 8:49 pm
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
Posted: Fri Mar 27, 2009 11:01 am
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
Posted: Fri Mar 27, 2009 12:18 pm
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.
Posted: Fri Mar 27, 2009 1:12 pm
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
-----------------------------------------
solution to find and replace question
Posted: Fri Mar 27, 2009 2:22 pm
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.
Posted: Fri Mar 27, 2009 3:28 pm
by ben_josephs
Why are you putting the #s in, only to take them out again?
Posted: Fri Mar 27, 2009 7:34 pm
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 #?
Posted: Fri Mar 27, 2009 9:04 pm
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.
Posted: Fri Mar 27, 2009 10:23 pm
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.