私は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
しかし今、私は各グループの最高のスコアを見つける方法がわかりません。
ありがとう