-1

I have an awk script here:

    BEGIN { count=0;sum=0;thresh_count=0; 
thresh=ARGV[2]} $3=="3"{count++;print $2,$1,$4,$5;sum+=$5;if($5>=thresh)thresh_count++} 
    END {if(NR>0)print "\nThe average grade for the class is "sum/count".";
    print "Number of grades past threshold: " thresh_count;}

and I'd like to be able to pass an argument from the command line into the variable 'thresh'.

When I enter:

awk -f script.awk input.txt 90

into the terminal, however, I get an error that "90" is not a file or a directory. How can I get it to pass in "90" as an argument rather than trying to read it as an additional file to run the script on? Any help would be greatly appreciated!

duckyPluck
  • 177
  • 8

1 Answers1

2

Arguments after the script are taken to be files to process. Reading them with ARGV doesn't change that.

You use the -v option to set awk variables.

awk -f script.awk -v thresh=90 input.txt
Barmar
  • 741,623
  • 53
  • 500
  • 612