1751792 Members
4924 Online
108781 Solutions
New Discussion юеВ

Re: Help needed in perl

 
SOLVED
Go to solution
Srikanth Arunachalam
Trusted Contributor

Help needed in perl

I need to process inbound feed file with name as XXX_DD_MM_YYYY_GnnnnVnn.DAT and split them based on country code.

Kindly help me with what I need to do to dynamically create a file in the script and based on country UK, I need to dynamically create XXX_UK_DD_MM_YYYY_GnnnnVnn.DAT. I have multi dated files present in my inbound feed area.

I can do this in shell as well, but I believe processing of large feed file is quicker in perl than shell, kindly correct me if I am wrong.
8 REPLIES 8
Srikanth Arunachalam
Trusted Contributor

Re: Help needed in perl

An earlier response to above query will be much apprecialted.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help needed in perl

Hi:

Sample input and sample, expected output go a long way to offering decent suggestions.

In your example, how would I know that the country code is "UK"?

Regards!

...JRF...
Srikanth Arunachalam
Trusted Contributor

Re: Help needed in perl

It is based on the column position within each record. The country code seems to be available between position 11-13 on each record. Each record in my file XXX_DD_MM_YYYY_GnnnnVnn.DAT would read as
4000000001600XXXXXXXXXXX
If 600, then I would need to copy it to a file by name XXX_600_DD_MM_YYYY_GnnnnVnn.DAT.

Thanks,
Srikanth
Michael Steele_2
Honored Contributor

Re: Help needed in perl

Hi

What do you have so far? Any open or close file statements? Any variable file names?
Support Fatherhood - Stop Family Law
Srikanth Arunachalam
Trusted Contributor

Re: Help needed in perl

Hi,

Currently I have the following script.
I would like to create new tgtfile control file based on current file of datafile.

#!/usr/bin/perl
my ($datafile, $tmp, $i, $line, $columns, $x, $y);
my (@col, @dataline, @cell, %col);


$datafile = $ARGV[0]; # pick up data file name from command line
$targetfile = $ARGV[1];
$tgtfile = $ARGV[2];
$cntfile = $ARGV[3];

open (DATAFILE, "<$datafile") or die "Failed to open $datafile for input";

open(TGT1,">$targetfile") or die $!;

open(TGT2,">$tgtfile") or die $!;

while($line = )
{
chomp($line);
next unless($line =~ m/\S+/);

@array = split("",$line);

if(($array[11] == 2)&&($array[12] == 7))
{
$line = join("",@array);
print TGT1 $line ."\n";
$x++
}
else
{
$line = join("",@array);
print TGT2 $line ."\n";
$y++
}
}
printf ("%010d\n",$x);
printf ("%010d\n",$y);

close DATAFILE;
close TGT1;
close TGT2;

H.Merijn Brand (procura
Honored Contributor

Re: Help needed in perl

>#!/usr/bin/perl

You miss these:

use strict;
use warnings;

>my ($datafile, $tmp, $i, $line, $columns, $x, $y);

declare the variables where you use and initialize them. Not as a bunch of unrelated variables at the top of your script.

>my (@col, @dataline, @cell, %col);

You don't use these. anywhere.

>
>$datafile = $ARGV[0]; # pick up data file name from command line
>$targetfile = $ARGV[1];
>$tgtfile = $ARGV[2];
>$cntfile = $ARGV[3];

my ($datafile, $targetfile, $tgtfile, $cntfilr) = @ARGV;

>open (DATAFILE, "<$datafile") or die "Failed to open $datafile for input";

open my $dfh, "<", $datafile or die "$datafile: $!";

>open(TGT1,">$targetfile") or die $!;

open my $tfh1, ">", $targetfile or die "$tgtfile: $!";

>open(TGT2,">$tgtfile") or die $!;

open my $tfh2, ">", $tgtfile or die "$tgtfile: $!";

>while($line = )
>{

while (<$dfh>) {

>chomp($line);

chomp;

>next unless($line =~ m/\S+/);

\S+ is stupid if a single \S is enough to get it pass

m/\S/ or next;

>@array = split("",$line);
>if(($array[11] == 2)&&($array[12] == 7))
>{

I /think you do NOT want to do it like that.

Assuming there is some structure in the lines, you either want to use substr ($_, 11, 2) or (probably better) some sort of

my ($x, $y, $code, ...) = unpack "A5 A6 A2 ...", $_;
if ($code == 27) {

see 'man perlpacktut' for some more in-depth explanation.

>$line = join("",@array);

Why reassemble the line while you never modified it?

>print TGT1 $line ."\n";

print $tfh1 $_;
$x++;
}

>$x++
>}
>else
>{
>$line = join("",@array);
>print TGT2 $line ."\n";
>$y++
>}
else {
print $tfh2 $_;
$y++;
}
}

>printf ("%010d\n",$x);
>printf ("%010d\n",$y);

printf "%010d\n%010d\n", $x, $y;

>close DATAFILE;
>close TGT1;
>close TGT2;

close $dfh or die "$datafile: $!";
close $tfh1 or die "$targetfile: $!";
close $tfh2 or die "$tgtfile: $!";

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Help needed in perl

Hi:

If I understand correctly, I think this fits your needs:

# cat ./myassembly
#!/usr/bin/perl
use strict;
use warnings;
my $outfh;
my $count = 0;
while (<>) {
my ( $left, $right ) = $ARGV =~ m{(^..._)(.+)};
chomp;
my $match = substr( $_, 10, 3 ); #...zero-relative...
if ( $match eq '600' ) {
if ( $count <= 0 ) {
my $newname = $left . $match . '_' . $right;
open( $outfh, '>>', $newname )
or die "Can't open '$newname': $!\n";
$count++;
}
print $outfh "$_\n";
}
}
continue {
close $outfh if eof;
$count = 0;
}
1;

...You can pass multiple filenames on the command line for processing, too:

# ./myassembly XXX_DD_MM_YYYY_GnnnnVnn.DAT YYY_DD_MM_YYYY_GnnnnVnn.DAT ...

Regards!

...JRF...
Michael Steele_2
Honored Contributor

Re: Help needed in perl

Don't forget to award points to these two, hell, they did just write your script for you.
Support Fatherhood - Stop Family Law