Using TextPad to extract and rewrite flatfiles

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
stuntmusic
Posts: 3
Joined: Thu Jun 09, 2011 4:03 pm

Using TextPad to extract and rewrite flatfiles

Post by stuntmusic »

I am not sure this is something regex can do or another module within TextPad.

I have a flatfile which has multiple lines in it.

Each line represents a record.

For each record, data is written with xx-xx spaces, for example, first name might be 'contained' in spaces 127-145.

I can move to that 'space' using TextPad which allows me to validate the data.

Is there a way to write a regex expression, provided I know the 'space' values, to output a new file so data validation is more easily accomplished?

I do not have access to or ability to change the incoming flat file, I have to work with it as my source file.

Ideally, it would take data that looks like this

##41SmithMarc123Main Street AnytownST12345

to

ID ##41
LN Smith
FN Marc
AD1 123 Main Street
AD2
CI Anytown
ST ST
ZP 12345

I have to capture and extract data defined just by the location of the string (col 127) and the string length (127-145)

Is this possible?

Thanks in advance for the help.

= Marc
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Do you mean that your fields are fixed width?

The source of this page suggests that your input data looks like this:

Code: Select all

##41SmithMarc123Main Street      AnytownST12345
From this I'm making a wild guess that the fields have the (absurdly short) lengths of 4, 5, 4, 15, 5, 7, 2 and 5.

Try this (adjusting the widths where necessary):

Use "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Search | Replace... (<F8>):
Find what: ^(.{4})(.{5})(.{4})(.{15})(.{5})(.{7})(.{2})(.{5})
Replace with: ID \1\nLN \2\nFN \3\nAD1 \4\nAD2 \5\nCI \6\nST \7\nZP \8

[X] Regular expression

Replace All
stuntmusic
Posts: 3
Joined: Thu Jun 09, 2011 4:03 pm

Post by stuntmusic »

Ben,

The 'fields' are not fixed width. They vary from record to record, and the flat file sometimes uses spaces for a field that is not 'full', sometimes it uses zeros.

I do know the exact field lengths however, so the method you propose above might be exactly what I need.

I will try it and see what happens.

Thanks.
stuntmusic
Posts: 3
Joined: Thu Jun 09, 2011 4:03 pm

Post by stuntmusic »

I tried the search and replace method, but TextPad said
Cannot find regular expression ^(.{4})(.{5})(.{4})(.{15})(.{5})(.{7})(.{2})(.{5}) \n
I did set the editor to allow for POSIX regular expression syntax. I also restarted TextPad to see if that was the culprit.

I will keep trying. Is the POSIX engine built into TextPad or do I need to install it?
Post Reply