基于参数的bash计数发生

发布于 01-18 09:28 字数 2453 浏览 4 评论 0原文

我是Bash Shell的新手,我必须使用CSV文件进行脚本。

该文件是参与者,国家,体育和奖牌的列表。

执行脚本时,我应该将参数作为normanity(第3列)和sport(第8列)。脚本应归还该国家的参与者的数量,以及获得的奖牌数量。

获得的奖牌是每行9,10和11的列“金”“银”“青铜”的总和。

我不能使用GREP,AWK,SED或CSVKIT。

到目前为止,我有此代码,但我遇到了奖牌计数部分。

nacionality=$1
sport=$2
columns= cut -d, -f 3,8 athletes.csv
echo columns | tr -cd $nacionality,$sport | wc -c

谁能帮我吗?

该文件为: https://github.com/github.com/flother/rio2016/blob/2016/blob/ master/adtheres.csv

文件的名称是script2_4.sh

输出的示例是:

./script2_4.sh POL rowing

 Participants, Medals


 26, 6

文件的示例:

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
736041664,A Jesus Garcia,ESP,male,1969-10-17,1.72,64,athletics,0,0,0,
532037425,A Lam Shin,KOR,female,1986-09-23,1.68,56,fencing,0,0,0,
435962603,Aaron Brown,CAN,male,1992-05-27,1.98,79,athletics,0,0,1,
521041435,Aaron Cook,MDA,male,1991-01-02,1.83,80,taekwondo,0,0,0,
33922579,Aaron Gate,NZL,male,1990-11-26,1.81,71,cycling,0,0,0,
173071782,Aaron Royle,AUS,male,1990-01-26,1.80,67,triathlon,0,0,0,
266237702,Aaron Russell,USA,male,1993-06-04,2.05,98,volleyball,0,0,1,
382571888,Aaron Younger,AUS,male,1991-09-25,1.93,100,aquatics,0,0,0,
87689776,Aauri Lorena Bokesa,ESP,female,1988-12-14,1.80,62,athletics,0,0,0,
997877719,Ababel Yeshaneh,ETH,female,1991-07-22,1.65,54,athletics,0,0,0,
343694681,Abadi Hadis,ETH,male,1997-11-06,1.70,63,athletics,0,0,0,
591319906,Abbas Abubakar Abbas,BRN,male,1996-05-17,1.75,66,athletics,0,0,0,
258556239,Abbas Qali,IOA,male,1992-10-11,,,aquatics,0,0,0,
376068084,Abbey D'Agostino,USA,female,1992-05-25,1.61,49,athletics,0,0,0,
162792594,Abbey Weitzeil,USA,female,1996-12-03,1.78,68,aquatics,1,1,0,
521036704,Abbie Brown,GBR,female,1996-04-10,1.76,71,rugby sevens,0,0,0,
149397772,Abbos Rakhmonov,UZB,male,1998-07-07,1.61,57,wrestling,0,0,0,
256673338,Abbubaker Mobara,RSA,male,1994-02-18,1.75,64,football,0,0,0,
337369662,Abby Erceg,NZL,female,1989-11-20,1.75,68,football,0,0,0,
334169879,Abd Elhalim Mohamed Abou,EGY,male,1989-06-03,2.10,88,volleyball,0,0,0,
215053268,Abdalaati Iguider,MAR,male,1987-03-25,1.73,57,athletics,0,0,0,
763711985,Abdalelah Haroun,QAT,male,1997-01-01,1.85,80,athletics,0,0,0,

I'm new to bash shell and I have to do a script with a csv file.

The file is a list of the participants, countries, sports and medals achieved.

when executing the script, I should give as parameters the nationality (column 3) and the sport (column 8). The script should return the amount of participants of that country for that sport, and the amount of medals achieved.

The amount of medals achieved is the sum of the columns "gold" "silver" "bronze" of each row which are columns 9,10 and 11.

I cannot use grep, awk, sed or csvkit.

So far, I have this code but I'm stuck with the medal counting part.

nacionality=$1
sport=$2
columns= cut -d, -f 3,8 athletes.csv
echo columns | tr -cd $nacionality,$sport | wc -c

Could anyone help me?

The file is: https://github.com/flother/rio2016/blob/master/athletes.csv

The name of the file is script2_4.sh

An example of the output is:

./script2_4.sh POL rowing

 Participants, Medals


 26, 6

A sample of the file:

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
736041664,A Jesus Garcia,ESP,male,1969-10-17,1.72,64,athletics,0,0,0,
532037425,A Lam Shin,KOR,female,1986-09-23,1.68,56,fencing,0,0,0,
435962603,Aaron Brown,CAN,male,1992-05-27,1.98,79,athletics,0,0,1,
521041435,Aaron Cook,MDA,male,1991-01-02,1.83,80,taekwondo,0,0,0,
33922579,Aaron Gate,NZL,male,1990-11-26,1.81,71,cycling,0,0,0,
173071782,Aaron Royle,AUS,male,1990-01-26,1.80,67,triathlon,0,0,0,
266237702,Aaron Russell,USA,male,1993-06-04,2.05,98,volleyball,0,0,1,
382571888,Aaron Younger,AUS,male,1991-09-25,1.93,100,aquatics,0,0,0,
87689776,Aauri Lorena Bokesa,ESP,female,1988-12-14,1.80,62,athletics,0,0,0,
997877719,Ababel Yeshaneh,ETH,female,1991-07-22,1.65,54,athletics,0,0,0,
343694681,Abadi Hadis,ETH,male,1997-11-06,1.70,63,athletics,0,0,0,
591319906,Abbas Abubakar Abbas,BRN,male,1996-05-17,1.75,66,athletics,0,0,0,
258556239,Abbas Qali,IOA,male,1992-10-11,,,aquatics,0,0,0,
376068084,Abbey D'Agostino,USA,female,1992-05-25,1.61,49,athletics,0,0,0,
162792594,Abbey Weitzeil,USA,female,1996-12-03,1.78,68,aquatics,1,1,0,
521036704,Abbie Brown,GBR,female,1996-04-10,1.76,71,rugby sevens,0,0,0,
149397772,Abbos Rakhmonov,UZB,male,1998-07-07,1.61,57,wrestling,0,0,0,
256673338,Abbubaker Mobara,RSA,male,1994-02-18,1.75,64,football,0,0,0,
337369662,Abby Erceg,NZL,female,1989-11-20,1.75,68,football,0,0,0,
334169879,Abd Elhalim Mohamed Abou,EGY,male,1989-06-03,2.10,88,volleyball,0,0,0,
215053268,Abdalaati Iguider,MAR,male,1987-03-25,1.73,57,athletics,0,0,0,
763711985,Abdalelah Haroun,QAT,male,1997-01-01,1.85,80,athletics,0,0,0,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云2025-01-25 09:28:36

这是一个纯粹的 bash 实现。构建从字段名称到位置 ($h) 的哈希:

#!/bin/bash

file=athletes.csv
nationality=$1
sport=$2

IFS=, read -a l < "$file"
declare -A h
for pos in "h${!l[@]}"
do
    h["${l[$pos]}"]=$pos
done

declare -i participants=0
declare -i medals=0
while IFS=, read -a l
do
    if [ "${l[${h["nationality"]}]}" = "$nationality" ] &&
        [ "${l[${h["sport"]}]}" = "$sport" ]
    then
    ((participants++))
        medals=$((
        $medals +
        "${l[${h["gold"]}]}" +
        "${l[${h["silver"]}]}" +
        "${l[${h["bronze"]}]}"
    ))
    fi
done < "$file"
echo "Participants, Medals"
echo "$participants, $medals"

以及包含前 4 行输入的示例输出:

$ ./script2_4.sh CAN athletics
Participants, Medals
1, 1

Here is a pure bash implementation. Build a hash from field name to position ($h):

#!/bin/bash

file=athletes.csv
nationality=$1
sport=$2

IFS=, read -a l < "$file"
declare -A h
for pos in "h${!l[@]}"
do
    h["${l[$pos]}"]=$pos
done

declare -i participants=0
declare -i medals=0
while IFS=, read -a l
do
    if [ "${l[${h["nationality"]}]}" = "$nationality" ] &&
        [ "${l[${h["sport"]}]}" = "$sport" ]
    then
    ((participants++))
        medals=$((
        $medals +
        "${l[${h["gold"]}]}" +
        "${l[${h["silver"]}]}" +
        "${l[${h["bronze"]}]}"
    ))
    fi
done < "$file"
echo "Participants, Medals"
echo "$participants, $medals"

and example output with the first 4 lines of input:

$ ./script2_4.sh CAN athletics
Participants, Medals
1, 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文