Shell-Script

計算 states.txt 中列出的州的平均面積

  • April 20, 2021

需要有人計算 中列出的州的平均面積(以平方英里為單位)states.txt

States.txt(文件):

State           Population   SqMi      Region 
------------------------------------------
California           39.37   163.7       West
Texas                29.36   268.6      South
Florida              21.73    65.7      South
New_York             19.33    54.7  NorthEast
Pennsylvania         12.78    46.1  NorthEast
Illinois             12.59    57.9    Midwest
Ohio                 11.69    44.8    Midwest
Georgia              10.71    59.4      South
North_Carolina       10.60    53.8      South
Michigan             9.97    96.7    Midwest

輸出應如下所示:

Average Area: 82.85 square miles

請注意,我的 awk 程序將像這樣執行:

awk –f <file.awk> states.txt 

像這樣的東西可以做這項工作:

BEGIN {sum=0} {sum+=$3} END {print "Average Area: "sum/(NR-2)" square miles"}

前兩行可以忽略,因為求和操作會將字元串計數為零值

引用自:https://unix.stackexchange.com/questions/645903