How to count item occurences in BASH array?

后端 未结 3 951
一整个雨季
一整个雨季 2021-01-14 22:12

I have an array ${myarr[@]} with strings. ${myarr[@]} basically consists of lines and each line constists of words.

world hello moo         


        
3条回答
  •  礼貌的吻别
    2021-01-14 22:37

    No need for an external program:

    count=0
    for word in ${myarr[*]}; do
        if [[ $word =~ hello ]]; then
            (( count++ ))
        fi
    done 
    
    echo $count
    

提交回复
热议问题