1830133 Members
2349 Online
109999 Solutions
New Discussion

KSH to PERL

 
SOLVED
Go to solution
Ricardo Bassoi
Regular Advisor

KSH to PERL

Hi,

I??m a beginner using Perl and I??d like to know how to write the ksh script in the attachment into Perl code.
It takes 2 hours using ksh.

Regards,

R.Bassoi
If you never try, never will work
5 REPLIES 5
Jordan Bean
Honored Contributor

Re: KSH to PERL

Let's see if we can speed up the KSH script first...

Opening the output file once and spawning cut six times during each iteration will kill the performance of any script.

Work with this:

#!/usr/bin/ksh
DATE=$(date)
while IFS=';' read VAR1 VAR2 VAR3 ...
do
case $VAR4 in
RJ*) VAR_DG=INTRA-RJ-2;;
...
*) print -u2 "$DATE -> INVALID UF ERROR: $VAR4";;
esac
print -u1 "CRECNT........"
done < file.csv > output.txt 2> cbase.rr

Jordan Bean
Honored Contributor

Re: KSH to PERL

Wait... We need to prevent printing a line if the UF is invalid:

Try this instead:

#!/usr/bin/ksh
DATE=$(date)
while IFS=';' read VAR1 VAR2 VAR3 ...
do
case $VAR4 in
RJ*) VAR_DG=INTRA-RJ-2;;
...
*) VAR_DG=''; print -u2 "$DATE -> INVALID UF ERROR: $VAR4";;
esac
if [ ${#VAR_DG} -gt 0 ]
then
print -u1 "CRECNT........"
fi
done < file.csv > output.txt 2> cbase.rr
Rodney Hills
Honored Contributor
Solution

Re: KSH to PERL

I'm note sure what the layout of your input file is precisely, but I've attached an equivalent perl code routine.

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: KSH to PERL

Rodney, some remarks. I bet you are a long time perl user, cause I see some perl4 here :)

=open(INP,"
missing double quote and replace || with or

open INP, "< arquivo1a.csv" or die "..."

=open(OUT,">output.txt);

again missing double qoute, and check return status

open OUT, "> output.txt" or die "...";

=%codemap=(
= "RJ","INTRA-RJ-2",
= "ES","INTRA-ES-2",
= :
= );

use lexicals and => for automatic quoting of key values in hash

my %codemap = (
RJ => "INTRA-RJ-2",
);

=while() {
= chop;

chop is evil. use chomp.

= ($var1,$var2,$var3,$var4,$var5)=split(";",$_);

wrong. splits first argument is a regular expression, not a string. For the two exceptions top that where the first argument *is* a string read the docs.

my ($var1, ...) = split m/;/; # $_ is default second arg for split

= $var1=$var1+0;

$var1 += 0;

= $var_uf=substr($var4,0,2);
= if ($var_dg=$codemap{$var_uf}) {
Enjoy, Have FUN! H.Merijn
Ricardo Bassoi
Regular Advisor

Re: KSH to PERL


Thanks to all !!!
If you never try, never will work