2041_22T2.Q5

针对特定的奖项,找出它的“最早获奖年份(Min)”和“最晚获奖年份(Max)”。然后在这个 [Min, Max] 的闭区间内,找出所有没有颁发该奖项的年份,并输出。

脚本接收两个位置参数:

$1:一个用于匹配奖项名称的正则表达式(Regex)。

$2:数据文件的路径(如 awards.psv)。绝对不能在代码里写死文件名(”You should not hard code the name of this file” )。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/dash

regex="$1"
file="$2"

years=$(grep -E "^(${regex})[|]" "$file" | cut -d "|" -f 2 | sort -nu )

if [ -z "$years"]; then
echo "No award matching '$regex'"
exit 0
fi

min=$(echo "$years" | head -n 1)
max=$(echo "$years" | tail -n 1)

curr=$min

while [ "$curr" -le "$max" ];do
if ! echo "$years" | grep -xq "$curr" ; then
echo "$curr"
fi
curr=$((curr + 1))
done