我需要打印16972 1684 173
,但+
不起作用,沒有+
它只刪除一位數字。查找在bash中替換數字
#!/bin/bash
numbers="16972,4 1684,15556 173,2"
echo $numbers
echo ${numbers//,[[:digit:]]+/}
我需要打印16972 1684 173
,但+
不起作用,沒有+
它只刪除一位數字。查找在bash中替換數字
#!/bin/bash
numbers="16972,4 1684,15556 173,2"
echo $numbers
echo ${numbers//,[[:digit:]]+/}
如果啓用extglob
外殼選項可以匹配模式的一次或多次出現:
$ shopt -s extglob
$ echo "${numbers//,+([0-9])/}"
16972 1684 173
見Pattern Matching在Bash的手冊。
可以使用sed
代替
$ sed -r 's/,[0-9]+//g' <<< $numbers
16972 1684 173
很好的答案。 @krish,請注意,bash參數擴展技巧不使用正則表達式,它們使用shell模式。閱讀eugene答案中的鏈接。 –