Operating System - HP-UX
1834926 Members
2316 Online
110071 Solutions
New Discussion

Re: fixed length file manipulation - is there a better way?

 
SOLVED
Go to solution
Scott Williams_5
Frequent Advisor

fixed length file manipulation - is there a better way?

we have many data files that are fixed-length files with fixed-length fields. I was tasked with masking out some or all of two fields within each record. I wrote a script that used "cut" to split the file into multiple slices (columns), then used sed to manipulate the data, and "paste" to paste the pieces back together.

I'm thinking that there's got to be a better way.

I've attached the script I used as my solution -- what would your solution have been?


Cheers

Scott
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: fixed length file manipulation - is there a better way?

My cut at this would have been a Perl script; it would easily do all of this and can handle long records w/o LF's. It would execute at least 100X faster than your cut-based solution.

Your other option (as long as the records are 3000 characters or less) is awk. It too would be much more efficient. The Gnu version of awk, gawk, can handle arbitrarily large records to get around the 3K limit.

Learn Perl; that's my first choice but I ain't gonna code it for you because that's really doing you no favor.
If it ain't broke, I can fix that.
Scott Williams_5
Frequent Advisor

Re: fixed length file manipulation - is there a better way?

Thanks A. Clay -- I don't the script, I just need some good advise, and you have provided that nicely

Thanks
H.Merijn Brand (procura
Honored Contributor

Re: fixed length file manipulation - is there a better way?

use perl and pack/unpack
no line length limit

foreach my $filename (glob "flatord.*") {
open my $out ">$filename.new" or die "$filename.new: $!";
local @ARGV = ($filename);
while (<>) {
my ($f1, $nr, $f2, $dt, $f3) = unpack "a768 A16 a160 A4 a*", $_;
$nr = sprintf"%s%04d","X"x16,$nr%10000;
$dt =~ s/./X/g;
print $out "$f1$nr$f2$dt$f3";
}
close $out;
print "="x40,"\n"


Should be about the same, uses no temp files, is a single bloody fast process. Didn't test

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: fixed length file manipulation - is there a better way?

Clay, I cannot type that fast .....

:)

And I also head to read and understand the attachment

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn