I would like to ask how to find 5 most often strings and number of their occurence in this case.
I have a cycle in bash script, in this cycle there is one variable which is changed every iteration to some string.
I need to be able to save to some variable(s) (probably array?) 5 most often strings together with the number of their occurence (second array?) to be able to work with this later in the script.
This is my trying code..
last=0 #index of the last string in the array
for i in ...
do
string=... #this is changed each iteration
placed=0 #checks whether the string has already benn placed
index=0
while [ "$placed" -ne 1 ] #searches if the string is not places through the array ARRAY
do
if [ "$last" -eq "$index" ] ; then # this should place the string at the end if it is not in the arraz already
ARRAY[index]="$string"
OCCURENCE[index]=1
(( index++ ))
(( last++ ))
break
fi
if [ "$string" == "$ARRAY[$index]" ] ; then
# here i have another array with the occurences and increment the same index there
(( OCCURENCE[index]++ ))
placed=1
fi
(( index++ ))
done
done
If the main for loop will have 10 iterations, and there will be strings
"hello 1"
"hello 2"
"hello 3"
"hello 1"
"hello 1"
"hello 2"
"hello 4"
"hello 5"
"hello 6"
"hello 2"
I would like to have array with strings
"hello 1"
"hello 2"
"hello 3"
"hello 4"
"hello 5"
"hello 6"
And occurance array
3
3
1
1
1
1