Page 1 of 1
Search and Replace Question
Posted: Thu Jan 17, 2008 7:54 pm
by wmhowell
I'm trying to line up columns in a text doc that has omitted fields for some data elements.
Here's the type of command I'm trying to implement
"If there is not a hyphen in column 18 [column 18 <> "-"] then insert 17 blank spaces starting in column 14". Some additional info which may help. If there is a hyphen in column 18 then the characters in columns 14 thru 17 are always numeric.
Below is the "as is" BEFORE picture and the "to be" AFTER picture of how the doc should look. [Note: b's are used to represent the blank spaces.]
BEFORE
232-00 XDOZZ 54184 D3467-1 RAIL
232-00 XDOZZ 54184 D3467-2 RAIL RH SEE
232-01 PAOZZ 5304-00-151-1420 88044 AN4-14A . BOL
232-02 PAOZZ 5310-00-844-4872 80205 MS21043-4 . N
232-10 PAOZZ 5310-00-660-3381 96906 MS21083N5 . N
232-11 PAOZZ 5310-00-167-0820 80205 NAS1149F0563P
232-12 PAOZZ 3040-01-258-5185 54184 B4123-12 .
232-13 PAOZZ 5342-01-265-9125 54184 B4583-1 . MOU
232-14 XDOZZ 54184 D3792-1 . RAIL GUIDE SUB A
232-14 XDOZZ 54184 D3792-2 . RAIL GUIDE SUB,A
AFTER
232-00 XDOZZ bbbbbbbbbbbbbbb 54184 D3467-1 RAIL
232-00 XDOZZ bbbbbbbbbbbbbbb 54184 D3467-2 RAIL RH SEE
232-01 PAOZZ 5306-00-151-1420 88044 AN4-14A . BOL
232-02 PAOZZ 5310-00-844-4872 80205 MS21043-4 . N
232-10 PAOZZ 5310-00-660-3381 96906 MS21083N5 . N
232-11 PAOZZ 5310-00-167-0820 80205 NAS1149F0563P
232-12 PAOZZ 3040-01-258-5185 54184 B4123-12 .
232-13 PAOZZ 5342-01-265-9125 54184 B4583-1 . MOU
232-14 XDOZZ bbbbbbbbbbbbbbb 54184 D3792-1 . RAIL GUIDE SUB A
232-14 XDOZZ bbbbbbbbbbbbbbb 54184 D3792-2 . RAIL GUIDE SUB,A
thanks for your help
Posted: Thu Jan 17, 2008 9:56 pm
by Bob Hansen
This gives the visual results to match your before and after.
The count of "b" is only 15 vs. 17, so not sure of "space" count.
Adjust accordingly by replacing the "b" with "space" as needed
Search for: ^(.{13})(.{4})([^-]{1})
Replace with: \1bbbbbbbbbbbbbbb \2\3
Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------
Posted: Thu Jan 17, 2008 11:10 pm
by wmhowell
Man, that's works great, Bob! I never would have come up with that.
40,000 lines of text. Formatted. Elapsed time: 5 minutes
I did the first 2000 lines manually before posting. Elapsed time: 1 hr.

Posted: Fri Jan 18, 2008 12:59 am
by Bob Hansen
I'm impressed that you got 2000 lines done manually in one hour!

Here's a second problem from the same data set
Posted: Fri Jan 18, 2008 3:07 pm
by wmhowell
I've gotten pretty fast at manually moving around data in Textpad. That's one of the reasons I like it so
much. The downside is it makes it too easy to do a brute force solution when an elegant Regular Expression
would do the heavy lifting better.
Here's another potential POSIX problem from the same data set. This one may be trickier than my earlier one.
The level of indenture in this data set is set by "b.b" where the b's are blank spaces. These dots appear after the part
number and before the description per the example below. [Note: the spaces are important. The "." appears elsewhere
in the doc and reformatting those would be a real mess.]
A bonus feature. If the number starting in column 19 is "00" or "000" then that line has zero levels of indenture
and, thus,no "b.b" identifier. In that case I want to move the first blank space after column 54 (where the partno
begins) to column 79.
Here's the Before picture of the data set.
Before
Volume 1 Fig. 121-00 XCOOO 78286 70204-42504-011 FLOOR INSTL /STA 288 TO 343/ SEE FIG. 75 FOR NHA @ 1
Volume 1 Fig. 121-01 PAOZZ 5305-01-077-1001 96906 MS21091-3004 . SCREW,CAP,SOCKET HE @ 36
Volume 1 Fig. 121-02 PAOZZ 5305-01-121-1650 96906 MS21091-3018 . SCREW,CAP,SOCKET HE @ 43
Volume 1 Fig. 121-08 PAOZZ 5210-01-112-5913 78286 70214-02555-108 . . PLATE,LEVEL @ 8
Volume 1 Fig. 121-09 PAOZZ 5325-00-403-8127 96906 MS3217-4062 . . RING,RETAINING @ 8
Volume 1 Fig. 121-10 PAOZZ 5325-01-117-7452 78286 70219-02554-105 . . STUD,TURNLOCK FASTE @ 8
Volume 1 Fig. 121-11 XAOOO 78286 70204-42504-042 . . BOND ASSY @ 1
Volume 1 Fig. 121-12 PAOZZ 5305-01-049-0624 96906 MS21091-3014 . . . SCREW,CAP,SOCKET HE @ 8
Volume 1 Fig. 121-13 PAOZZ 5310-01-352-9568 80205 NAS1149D0316K . . . WASHER,FLAT @ 8
The goal here is to line up the part descriptions in column 80 in a manner which captures the indenture level.
The first instance of "b.b", therefore should go in column 79.
When its all said and done the finished product should look like this.
[Yikes. When I preview this note all my formatting is washed away. In
TextPad this After document looks way better than it does here. What's
up with that?]
After
Volume 1 Fig. 121-00 XCOOO 78286 70204-42504-011 FLOOR INSTL /STA 288 TO 343/ SEE FIG. 75 FOR NHA @ 1
Volume 1 Fig. 121-01 PAOZZ 5305-01-077-1001 96906 MS21091-3004 . SCREW,CAP,SOCKET HE @ 36
Volume 1 Fig. 121-02 PAOZZ 5305-01-121-1650 96906 MS21091-3018 . SCREW,CAP,SOCKET HE @ 43
Volume 1 Fig. 121-08 PAOZZ 5210-01-112-5913 78286 70214-02555-108 . . PLATE,LEVEL @ 8
Volume 1 Fig. 121-09 PAOZZ 5325-00-403-8127 96906 MS3217-4062 . . RING,RETAINING @ 8
Volume 1 Fig. 121-10 PAOZZ 5325-01-117-7452 78286 70219-02554-105 . . STUD,TURNLOCK FASTE @ 8
Volume 1 Fig. 121-11 XAOOO 78286 70204-42504-042 . . BOND ASSY @ 1
Volume 1 Fig. 121-12 PAOZZ 5305-01-049-0624 96906 MS21091-3014 . . . SCREW,CAP,SOCKET HE @ 8
Volume 1 Fig. 121-13 PAOZZ 5310-01-352-9568 80205 NAS1149D0316K . . . WASHER,FLAT @ 8
If this is not readily doable in Regular Expressions, plan B is to use VB to parse it out.
Posted: Fri Jan 18, 2008 4:34 pm
by ben_josephs
You can't do that directly using TextPad's weak regular expression engine.
But you can move the descriptions way to the right with something like the following. Replace the underscores with spaces; the long line of underscores represents a sufficient number of spaces, say, 24.
Find what: ^(.{53}[^_]*)_
Replace with: \1________________________
[X] Regular expression
Replace All
You can then line the descriptions up by selecting column 54 and repeatedly replacing a single space by nothing. (Replace the underscore with a space.)
Find what: _
Replace with: [nothing]
[X] Regular expression
Replace All
Keep selecting
Replace All until it beeps.
Please don't use VB. It will make you ill.
Posted: Fri Jan 18, 2008 9:57 pm
by wmhowell
I see where this is going but still not sure how to get there.
The first command, to blow out all the data to the right, works great. It creates plenty of blank spaces to the right of column 80. The trick now is to iteratively reel in those lines back to the left until column 80 becomes a non "space". Once all of the spaces are squeezed out then comes the beep and the remainder is a neat column of data.
The question now becomes: How do I check column 80 specifically for a space (rather than the whole document) and, if it's still a space, replace the space with nothing.
I botched the Find and Replace from earlier and managed to blow away all my spaces. thank goodness for Undo.
And thanks again for all your help.
Posted: Fri Jan 18, 2008 10:48 pm
by ben_josephs
Select the whole column, as far up and down as you need. When you select text on more than one line, the check box Selected text is automatically selected.
Posted: Sat Jan 19, 2008 2:02 am
by wmhowell
OK. I'm at idiot. The whole time I've been using TP I've never tried to highlight a column. It works. Just like highlighting rows. My bad.
No more questions for a while. I'm sufficiently humbled.
Have a good weekend, guys.