我想从这个CSV文件的第二列捕捉中值按大陆分组在脚本awk文件:
Continent, Ratio
Asia,4.313743132
Europe,1.275057509
Africa,2.587215976
Europe,0.382270638
Africa,1.915438434
Latin America and the Caribbean,1.801681569
Latin America and the Caribbean,1.416472111
Asia,2.039169471
Latin America and the Caribbean,0.622595518
我试过了,但不起作用,因为我没有对CSV进行排序,甚至没有按大洲分组:
BEGIN {
FS = ","
Continent["Africa"];Continent["Asia"];Continent["Europe"];Continent["Latin America and the Caribbean"];
}
FNR>1 {
if ($1!="" && $2!="") {
a[i++]=$2
}
}END {
for (i in Continent) {
x=int((i+1)/2); if (x < (i+1)/2) print (a[x-1]+a[x])/2; else print a[x-1];
}
}
然而,我希望首先按大陆和数字(desc或asc,无关紧要)订购csv:
Continent, Ratio
Africa,2.587215976
Africa,1.915438434
Asia,4.313743132
Asia,2.039169471
Europe,1.275057509
Europe,0.382270638
Latin America and the Caribbean,1.801681569
Latin America and the Caribbean,1.416472111
Latin America and the Caribbean,0.622595518
最后得到各洲的中位数:
Continent, Median
Africa 2,2513
Asia 3,1764
Europe 0,8286
Latin America and the Caribbean, 1.4164
2条答案
按热度按时间h4cxqtbf1#
使用
gnu-awk
时,您可以使用以下解决方案:然后将其用作:
jv2fixgn2#
为了好玩,您可以在单个
gawk
脚本中编写此代码,但要与sort
协调,使用two-way pipe (or coprocess):中值.awk
结果