How to extract numbers from a string?

前端 未结 10 2093
青春惊慌失措
青春惊慌失措 2020-12-10 04:20

I have string contains a path

string=\"toto.titi.12.tata.2.abc.def\"

I want to extract only the numbers from this string.

To extrac

10条回答
  •  眼角桃花
    2020-12-10 05:02

    Here is a short one:

    string="toto.titi.12.tata.2.abc.def"
    id=$(echo "$string" | grep -o -E '[0-9]+')
    
    echo $id // => output: 12 2
    

    with space between the numbers. Hope it helps...

提交回复
热议问题