On 2/10/06, Chas Owens <[EMAIL PROTECTED]> wrote:
> On 2/10/06, Ken Hill <[EMAIL PROTECTED]> wrote:
> > I have the following perl script that reads a fixed-width file and
> > replaces values in various sections of the file.
> >
> > -------------------------------------------
> > open (IN, '< in.txt');
> > open (OUT, '> out_test.txt');
> >
> > while (<IN>) {
> >
> >           chomp;
> >
> >           $first_section = substr $_, 0, 381;     # extract the first
> > section of the record
> >           $facilityno = substr $_, 381, 10;    # extract the facilityno
> > field
> >           $second_section = substr $_, 391, 1056;        # extract the
> > second section of the record
> >           $requestor_section=" " x 500;
> >   # $requestor_section = substr $_, 1446, 499; # extract requestor
> > section of record
> >           $third_section = substr $_, 1946, 4748; # extract third
> > section of record
> >
> >           # print out the file with changed facilityno value ...
> >
> >           print OUT "$first_section$\0039007300$\$second_section$\
> > $requestor_section$\$third_section\n";
> >
> > }
> >
> > close (IN);
> > close (OUT);
> > ------------------------------------------------
> >
> > I want to place an "if...then" condition on the $facilityno value; such
> > that if the $facilityno value = 0000000000, then print out 999999999,
> > else print out the value of $facilityno.
> >
> > Any advice is very much appreciated.
>
> First, look at the pack and unpack functions.  They may make your life
> easier if you use a lot of fixed width files.  Second, just add a line
> like this:
>
> $facilityno = substr $_, 381, 10;    # extract the facilityno field
> $facilityno = 999999999 if $facilityno = 0;
>
> or you could do it as a one liner:
>
> perl -pe 's/^(.{381})0000000000/${1}999999999' in.txt > out_test.txt
>

I didn't read the code closely enough, the one liner would be

perl -ne 's/^(.{381})0000000000/${1}999999999/; $,=$\; print unpack
"a381 a10 a1056 a499 a4748"'

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to