Page 1 of 1

replace two specific column blocks in a fixed width file

Posted: Sat Aug 08, 2009 9:27 pm
by sabreena98
Hi guys, I have searched for a few hours on here, and either I am not sure what to call what I am trying to do or it is not here. I apologize in advance if it exists somewhere. I should say that my file is a fixed width file, which is why I am trying to work with position numbers.

I have 21 text files which vary from 3000 to 170000 records. I am trying to clear two large blocks of text in different areas of the file to be blank. I have used fill block and replacing the selections with the blank spaces, but it started getting hard to scroll through the files twice once they got larger than 50000 records. I want to clear the text between 135 and 606 and then 831 through the end, which is 1012. There are no specific text strings that repeat in the file as markers, but since it is a fixed width file I am able to use the spaces/columns.

I would appreciate any help that can be offered. Thanks in advance

edited:

I think I figured out how to get rid of the last bit of it: .{147}$.

I also think I figured out how to select the first section I want to get rid off:^(.{135}).{606} , but cannot figure out how to get it to replace that specific section with the empty spaces.

Posted: Sun Aug 09, 2009 2:14 am
by Bob Hansen
Here is a quick and dirty UNTESTED RegEx that should be close enough to be modified for your positions:

This Search RegEx string makes four groups, Group1 is position 1-15, Group 2 is position 16-25, Group 3 is position 26-40, Group 4 is positions 41-50;
The replacement string replaces groups 2 and 4 with the same number of space chars. OR, it clears the text between positions 15 and 26 and between positions 40-51.

Change the numbers to match your needs.

Find what: ^(.{15})(.{10})(.{15})(.{10})
Relace with: \1__________\3__________

Note that "_" represents a space char.

Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------

Posted: Sun Aug 09, 2009 5:14 am
by sabreena98
Hi Bob,

Thanks for the reply. I tried it and it didn't work. I changed the numbers to correspond with my full flat file length (I think that is what your example showed) then in the replace portion I put in empty spaces corresponding with those sections but I got an error message that it could not find the regular expression. I did check that I had my posix checked off in the editor section b/c that usually seems to be what causes that message. It is possible that I have to put too many spaces in?

This is what I plugged into the system:
Find what: ^(.{179})(.{427})(.{225)(.{147})
Replace with: \1
XX\3
XX

I put the xx in at the ends just to recognize the end to my blank spaces, but made sure to remove them from the actual replace field.


I think I figured it out , was missing a }in the third grouping…nope put that in and it didn’t fix it…still messing around with it

I think I got it 

I now have

^(.{178})(.{427})(.{225})(.{147})
\2 \4

Nope still fiddling

I need to keep section one and section three, which would mean I need to replace 2 and 4….but something is still not right, I somehow am skewing the spacing in the fixed file….


so swapped it back to the 1, 3 scenario but spacing is still off it is like the file shifts to the left, like I am not replacing everything I remove...

Posted: Sun Aug 09, 2009 5:52 am
by sabreena98
SUCCESS (I think at least it looks good to me so far)

I kept adding spaces in the rplace field until my positions lined up again :) (I think I ended up adding like 5 or 6)


S= ^(.{178})(.{427})(.{225})(.{147})
R= \1 with 427 spaces after and \3 with 147 spaces
Thank you so much!!!

Posted: Sun Aug 09, 2009 9:19 am
by ben_josephs
Here's a handy way to create a string of 427 spaces:

Create a new document.
Turn on block select mode: Configure | Block Select Mode
Go to column 427: Search | Go To... | Coulmn 427
Create the spaces: <Space>
(Turn off block select mode)
Select the spaces: <Shift+Home>
(The satus bar will show 1 lines, 427 columns selected.)
If you want to see the spaces: View | Visible Spaces
Copy them anywhere you like.

Edit: Corrected typo.

Posted: Sun Aug 09, 2009 2:59 pm
by Bob Hansen
Glad it worked for you sabreena98.

And thanks to ben_josephs for the handy way to select the space characters.

Sometimes it is easier to type out the Search/Replace strings on lines in TextPad so they are large enough to read/edit. Then just copy/paste them into the Search/Replace fields.

Posted: Sun Aug 09, 2009 4:42 pm
by sabreena98
you both rock.

Ben that was much easier than what I actually did. I did use textpad but used my spacebar till I got the required number of spaces. I did edit the formula that bob gave me in textpad, but mostly because unlike wordpad or notepad it has that very useful counter for the flat files.

Thanks again. I will probably be back while I try to learn the full capabilities of Textpad.