的最好成績,我用的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」)的分數分開,並從每個分組中選出得分最高的字符串。
在這種情況下,對於組「go.to.sleep」
,對於組「have.a.nice.day」和「go.to.sleep.b」將是「have.a.nice.day.c」 所以我認爲最好的辦法是將元素分開並遞歸地賦予它們變量。像這樣:
#!/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
,但現在我不知道如何找到每個組的最好成績。
謝謝
什麼是最終目標?效率還是學習bash? –
最終目標是學習bash – buntuser