如何将命令行参数传递到awk脚本中



我在这里有一个awk脚本:

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;}

我希望能够将一个参数从命令行传递到变量"thresh"中。

当我输入时:

awk -f script.awk input.txt 90

然而,在终端中,我得到一个错误,即"90"不是文件或目录。如何将它作为参数传入"90",而不是将其作为运行脚本的附加文件读取?如有任何帮助,我们将不胜感激!

脚本之后的参数将作为要处理的文件。用ARGV阅读它们并不能改变这一点。

您可以使用-v选项来设置awk变量。

awk -f script.awk -v thresh=90 input.txt

最新更新