Converting awk to perl


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Converting awk to perl
# 1  
Old 11-07-2019
Converting awk to perl

Hello. I'm trying to convert an awk script I wrote to perl (which I just started self-teaching). I tried the a2p command but I couldn't make sense of most of it.



Here was the awk code:


Code:
BEGIN{

FS = ","
print "NAME\tLOW\tHIGH\tAVERAGE"
a=0

}

{

if(a==0){
    a+=1
}

else{

    if(high[$3]<$4)
        high[$3] = $4

    else if (low[$3]>$4 || low[$3]==0)

        low[$3] = $4

        average[$3] += $4

        count[$3] += 1
}

}

END{

for(b in high){

average[b] = average[b]/count[b]
printf "%s\t%d\t%d\t%.2f\n",b, low[b], high[b], average[b]
}
}


So far this is what I got:


Code:
#!/usr/bin/perl

print "NAME\tLOW\tHIGH\tAVERAGE\n";
$a=0;

if($a==0){
        $a+=1
}

elsif{
   
}


I'm not sure how to convert this part to perl:


Code:
    

else{


if(high[$3]<$4)
        high[$3] = $4

    else if (low[$3]>$4 || low[$3]==0)

        low[$3] = $4

        average[$3] += $4

        count[$3] += 1
}

}


The awk code was to use a .csv file as input thus the $3 and $4
# 2  
Old 11-07-2019
Your turn next:
I got your awk code to run against a dummy input file. I cannot see why your sample ran for you - however after fixing syntax I got an awk source file t.awk. t.lis is the file created by a2p. You need to work on reconciling the a2p output.

I ran this command with some errors as below:
Code:
./a2p.exe t.awk > t.lis
Unrecognized character ' in file t.awk line 2--ignoring.   just remove the "awk" and tic marks I had them in there for convenience
Unrecognized character ' in file t.awk line 36--ignoring.
Please check my work on the 2 lines I've marked with "#???".  This needs looking at.
The operation I've selected may be wrong for the operand types.

t.awk is:
Code:
awk '
   BEGIN{
     FS = ","; print "NAME\tLOW\tHIGH\tAVERAGE"; a=0;
   }
 # main   
 {
   if(a==0)
   {
     { a+=1}
   }
   else
   {
       if(high[$3]<$4)
       {
           high[$3] = $4
       }
       else 
       if (low[$3]>$4 || low[$3]==0)
       {
           low[$3] = $4
           average[$3] += $4
           count[$3] += 1
       } 
   }
 }  
 #end main  
 END {
     
     for(b in high)
     {
       average[b] = average[b]/count[b]
       printf ("%s\t%d\t%d\t%.2f\n",b, low[b], high[b], average[b])
     }
   }
  '



t.lis is the perl output with minor issues I cannot fix.
Code:
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
			# this emulates #! processing on NIH machines.
			# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
			# process any FOO=bar switches

$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

$FS = ',';
print "NAME\tLOW\tHIGH\tAVERAGE";
$a = 0;

# main   

while (<>) {
    chomp;	# strip record separator
    @Fld = split(/[,\n]/, $_, -1);
    print $_ if $awk;

    if ($a == 0) {
	{
	    $a += 1;
	}
    }
    else {
	if ($high{$Fld[(3)-1]} lt $Fld[(4)-1]) {	#???
	    $high{$Fld[(3)-1]} = $Fld[(4)-1];
	}
	elsif ($low{$Fld[(3)-1]} gt $Fld[(4)-1] || $low{$Fld[(3)-1]} == 0) {	#???
	    $low{$Fld[(3)-1]} = $Fld[(4)-1];
	    $average{$Fld[(3)-1]} += $Fld[(4)-1];
	    $count{$Fld[(3)-1]} += 1;
	}
    }

    #end main  
}

foreach $b (keys %high) {
    $average{$b} = $average{$b} / $count{$b};
    printf "%s\t%d\t%d\t%.2f\n", $b, $low{$b}, $high{$b}, $average{$b};
}

These 2 Users Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting awk to perl

Hello. I'm currently teaching myself Perl and was trying to turn an awk code that I had written into Perl. I have gotten stuck on a particular part and a2p has not helped me at all. The task was to take a .csv file containing a name, assignment type, score and points possible and compute it into a... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Simple perl help - converting numbers

Hi friends, I'm very new to perl and got some requirement. I've input numbers which has size of 17 characters like below: -22500.0000000000 58750.00000000000 4944.000000000000 -900.000000000000 272.0000000000000 I need to convert these numbers from negative to positive and positive... (4 Replies)
Discussion started by: ganapati
4 Replies

4. UNIX for Advanced & Expert Users

problem with converting time using perl

Hello, I have an AIX 5.3 system and i created a script to get the last login of users. The script goes like this: LAST_LOGIN=`lsuser -a time_last_login $cur_user` TIME_LOGIN=`perl -e 'print scalar localtime("$LAST_LOGIN")'` Actually what i do in these two lines is to set a variable... (2 Replies)
Discussion started by: omonoiatis9
2 Replies

5. Shell Programming and Scripting

PERL- converting exponent value to floating point

Hi Friends, I've an exponent value like, $val="9.57669e-05"; I want to convert this value to floating point value in PERL scripting. I tried googling for the solution, and also asked many perl friends. Unfortunately, I didn't get answer. Could you please help me? Thanks in advance... (4 Replies)
Discussion started by: ganapati
4 Replies

6. Shell Programming and Scripting

Converting perl to exe

Hello All, I want to convert .pl to .exe with all include libraries like: WIN32/OLE..... Please suggest me how i will proceed for this.... (7 Replies)
Discussion started by: suvenduperl
7 Replies

7. Shell Programming and Scripting

Converting string to date in perl

Hi, I need convert a date string to date. For eaxmple $last_date=6/2/2009 and I want to change the format of the above mentioned date to "Jun 2 2009 12:00AM". Do we have any functionality or staright method to convert to the desired format? (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

8. Shell Programming and Scripting

Converting html to pdf perl

Hi All, I have a requirement of converting an html form into pdf using perl. The html form contains images, tables and css implementation. I tried using various perl modules but failed to achive the target. I succeeded in generating a pdf from the html file using... (2 Replies)
Discussion started by: DILEEP410
2 Replies

9. Shell Programming and Scripting

Converting Perl code to shell

I have a perl code that runs like Code I sub p { if ($d >= $D) {return} printf "%3s"x$d++," "; printf "%s%s\n",$_,$h{$_}?" ** ":""; if (!$h{$_}) { $h{$_}=1; map {p($_)} @{$s{$_}} } $d-- } ($Set,$Job,$Num,$D) = (@ARGV); map {shift} 0..3; (8 Replies)
Discussion started by: zainravi
8 Replies

10. UNIX for Dummies Questions & Answers

Converting bash shell to perl

Does anyone know how to convert this bash shell script to perl? for i in `ls -l *pgp` do `usr/bin/gpg --passphrase-fd 0 $i < .sunspot` done Thanks! Any help would be appreciated. I'm new to Linux and perl. (4 Replies)
Discussion started by: freak
4 Replies
Login or Register to Ask a Question