Create a script to manipulate a flat file
am 07.01.2008 04:45:00 von 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
Re: Create a script to manipulate a flat file
am 07.01.2008 06:50:21 von jurgenex
mrbungle50 wrote:
>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
Re: Create a script to manipulate a flat file
am 07.01.2008 06:53:52 von someone
mrbungle50 wrote:
>
> 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 ( ) {
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;
>xeg;
print;
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall