我正在试验 bash 脚本,需要帮助解决这个问题:
我在文本文件中有以下数据:(test.txt)
have.a.nice.day.a=42and55
have.a.nice.day.b=0
have.a.nice.day.c=55and67
go.to.sleep.a=111
go.to.sleep.b=2and122and33
go.to.sleep.c=64
我想将字符串与其匹配的分数分开,并将分数与其分隔符(在本例中:“and”)分开,并从每个组中选择得分最高的字符串。
在这种情况下,组“have.a.nice.day”将是“have.a.nice.day.c”,组“go.to.sleep”将是“go.to.sleep.b”,
所以我想最好的办法是分离元素并递归地为它们分配变量。像这样:
#!/bin/bash
names=$(cat test.txt | grep -o -P '.+(?==\d+)')
for name in $names
do
echo -n "$name"" "
scores=$(cat test.txt | grep -o -P '(?<='$name'=).+')
for scores_group in $scores
do
single_score=$(echo $scores_group | grep -o -P '\d+')
for score in $single_score
do
echo -n "$score"" "
done
echo
done
done
输出将是:
have.a.nice.day.a 42 55
have.a.nice.day.b 0
have.a.nice.day.c 55 67
go.to.sleep.a 111
go.to.sleep.b 2 122 33
go.to.sleep.c 64
但现在我不知道如何为每组找到最好的分数。
谢谢