Counting the number of dots in a string

前端 未结 6 1105
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 10:52

How do I count the number of dots in a string in BASH? For example

VAR=\"s454-da4_sd.fs_84-df.f-sds.a_as_d.a-565sd.dasd\"

# Variable VAR contains 5 dots
         


        
6条回答
  •  轮回少年
    2021-01-11 11:20

    VAR="s454-da4_sd.fs_84-df.f-sds.a_as_d.a-565sd.dasd"
    echo $VAR | tr -d -c '.' | wc -c
    

    tr -d deletes given characters from the input. -c takes the inverse of given characters. together, this expression deletes non '.' characters and counts the resulting length using wc.

提交回复
热议问题