I need some advice with working with a fixed length file that uses two different formats, the format switches every other line in the file. Here is an example of the file contents
The lines that have "03" as the first two characters match the following patterns: 02:10:33:15:19:10:3:18:6:4 While the other lines match this pattern: 02:98:11:9 Is using read and unpack the best way to approach this? The way that I am currently working with this I don't think this will work here becuase it loads in a file that has the pattern that matches the currently loaded file, in all I am working with four files and this is the only one that differs like this.03002068454210482 000000004204.572011-04-14 + 19:53:41I NTERNET C 750467375 ^M 0214833 + G02042954 ^M 03002068703214833 000000002558.662011-04-15 + 08:17:19I NTERNET C 761212737 ^M 0211561 + 05601207284 ^M 03002068802911561 000000001463.702011-04-15 + 08:40:52I NTERNET C 719807216 ^M 029911 + 00100275296 ^M
#!/usr/bin/perl use strict; use warnigns; my $filename = 'fixedfile.txt'; my $datname = $filename; $datname =~ s/\.txt//g; my $fc = 0; my @fla; my @fna; open my $dat, '<', "$datname.dat"; while (<$dat>) { chomp; my @fields = split(/\|/); foreach(@fields) { my ($field, $length) = split(/\:/, $_); if ( $length < 1 ) { $length = 1; } $fla[$fc] = $length; $fna[$fc] = $field; $fc++; } } close $dat; open my $fixedfile, '<', "$filename"; while (<$fixedfile>) { chomp; s/\r|\n//g; s/^M//g; s/^\s*//; s/\s*$//; my $line = $_; my $dc = 0; my $start = 0; foreach (@fna) { my $garbage = substr($line,$start,$fla[$dc]); $garbage =~ s/\'//g; $garbage =~ s/\"//g; $garbage =~ s/\\//g; $garbage =~ s/\(//g; $garbage =~ s/\)//g; $garbage =~ s/^\s*//; $garbage =~ s/\s*$//; $start = $start + $fla[$dc]; $dc++; } close $fixedfile; }
In reply to Working with fixed length files by vendion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |