Create a script to manipulate a flat file

M

mrbungle50

Hello all,

I have a question about designing a script that does two things. My
skills in script writing are limited so will need you to be patient
please.

I have a file downloaded to a server (Linux box) that contains set
length data in it. for example name, phone_number, address, zip_code,
balance

this file is what goes into an application whcih then translates the
data and uses it according to certain criteria set in the data in the
file.

What I need to achieve is.....

I want to read the zip_code on one row and if the zip_code is
(example)9000 I want to add a zone_code of (example)09 to the
beginning of the phone_number section.

The file is not delimited by commas etc, but mainly by character
spacing.

So if a row in the flat file reads the following:

987412wallabywayPerthWA9077000094730308JohnCitizen

that would be account number, house No, street, suburb/city, state,
zip code, leading 3 zeros, phone number, first name, last name.

I want to check if the zip code is 9XXX then remove the last 2 leading
zeros and insert the zone code in it's place.

The reason for this to put it in context is in Australia we have
telephone area codes that span different states. eg: 08 can be for
south Australia and the northern territory. But they are on different
time zones (only an hour) but if the company calling then customer
calls outside of the hours they are liable for massive fines for doing
so.

hence why I need to configure by zip code.

If you can find a script that would do this, I would be eternally
grateful.

Yours in hope

Craig

MrBungle50
 
J

Jürgen Exner

mrbungle50 said:
I want to read the zip_code on one row and if the zip_code is
(example)9000 I want to add a zone_code of (example)09 to the
beginning of the phone_number section.

The file is not delimited by commas etc, but mainly by character
spacing.

I do not understand. What do you mean by "character spacing"? Fixed
positions, like that zip code is always e.g. character 25 to 29?

So if a row in the flat file reads the following:

987412wallabywayPerthWA9077000094730308JohnCitizen

that would be account number, house No, street, suburb/city, state,
zip code, leading 3 zeros, phone number, first name, last name.

The fixed position I suspected above seems to be difficult, because street
and city names have a tendency to not be of equal length for all streets and
cities.
How do _YOU_ know, where the zip code starts and ends? What is the rule that
_YOU_ as a human apply to find that zip code?

jue
 
J

John W. Krahn

mrbungle50 said:
I have a question about designing a script that does two things. My
skills in script writing are limited so will need you to be patient
please.

I have a file downloaded to a server (Linux box) that contains set
length data in it. for example name, phone_number, address, zip_code,
balance

this file is what goes into an application whcih then translates the
data and uses it according to certain criteria set in the data in the
file.

What I need to achieve is.....

I want to read the zip_code on one row and if the zip_code is
(example)9000 I want to add a zone_code of (example)09 to the
beginning of the phone_number section.

The file is not delimited by commas etc, but mainly by character
spacing.

So if a row in the flat file reads the following:

987412wallabywayPerthWA9077000094730308JohnCitizen

This doesn't look like "set length data". Is that really what your data
looks like?

that would be account number, house No, street, suburb/city, state,
zip code, leading 3 zeros, phone number, first name, last name.

I want to check if the zip code is 9XXX then remove the last 2 leading
zeros and insert the zone code in it's place.

Just off the top of my head, (UNTESTED), something like this may work:

my %zone_codes = (
9000 => '09',
# more zone codes here
);

while ( <FILE> ) {
s<
^ # start of line
(\d+ # account number, house No?
\D+) # street, suburb/city, state?
(\d{4}) # 4 digit zip code?
000 # 3 leading zeros
sprintf '%s%s%03d',
$1, $2,
exists $zone_codes{ $2 } ? $zone_codes{ $2 } : 0;

print;
}



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top