我正试图从样本中找到土地面积百分比最大的县。然后,我尝试打印县和陆地区域的名称“ PERCENTAGE”。
BEGIN { FS="\t"}
BEGIN {mostland = 0} {countyname} {landpercent = $4 /($4+$3)} {if (landpercent > mostland) mostland = landpercent; countyname = $1}
END {if (NR) print countyname " has the largest percentage of land at " mostland "%"}
My test.txt
largest 10000 100 10
medium 5000 50 5
small 1000 25 1
它为“最大”显示正确的百分比。但是它返回“ small”作为县名。
“小土地所占比例最大,为.0909%”
答案 0 :(得分:0)
一些包围问题。 BEGIN {}
需要在每行之前设置变量。您正在按行重置。另外,您的if ()
都需要两个子句中的{}
。
尝试一下:
$ awk 'BEGIN { FS="\t"; mostland = 0; countyname = ""} {landpercent = $4 /($4+$3)} {if (landpercent > mostland) {mostland = landpercent; countyname = $1}} END {print countyname " has the largest percentage of land at " mostland "%"}' infile.txt
largest has the largest percentage of land at 0.0909091%