更新时间:2023-01-22 19:55:29
这是 sed
的一种方式:
$ echo $file | sed 's/[^0-9]*//g'
123
$ echo "123 he23llo" | sed 's/[^0-9]*//g'
12323
或者使用纯 bash
:
$ echo "${file//[!0-9]/}"
123
$ file="123 hello 12345 aaa"
$ echo "${file//[!0-9]/}"
12312345
要将结果保存到变量本身,请执行
To save the result into the variable itself, do
$ file=$(echo $file | sed 's/[^0-9]*//g')
$ echo $file
123
$ file=${file//[!0-9]/}
$ echo $file
123