我有我正經過值bash腳本 - 查找替換多個值
我要剝去傳遞給腳本值前綴一個非常簡單的bash腳本。
從傳遞價值的作品和帶test-
..
IN=$1
arrIN=(${IN//test-/})
echo $arrIN
所以測試12345 12345返回
反正是有修改這一所以它會刪除或者test-
或local-
?
我已經試過:
arrIN=(${IN//test-|local-/})
但是,這並不會工作。
感謝
我有我正經過值bash腳本 - 查找替換多個值
我要剝去傳遞給腳本值前綴一個非常簡單的bash腳本。
從傳遞價值的作品和帶test-
..
IN=$1
arrIN=(${IN//test-/})
echo $arrIN
所以測試12345 12345返回
反正是有修改這一所以它會刪除或者test-
或local-
?
我已經試過:
arrIN=(${IN//test-|local-/})
但是,這並不會工作。
感謝
如果你想改變 「測試 - 」 或 「局地」 到 「」 ,你可以使用如下命令:
awk '{gsub(/test-|local-/, ""); print}'
您可以使用sed
,並得到確切的結果
IN=$1
arrIN=$(echo $IN | sed 's/[^-]\+.//')
echo $arrIN
你可以用extglob
激活做到這一點:
shopt -s extglob
arrIN=(${IN//+(test-|local-)/})
從man bash:
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
'$ {VAR ## * - }'從去年得到部分'-'來字符串的結尾。但是你正在使用數組符號,所以目前還不清楚這是否足夠/ – fedorqui 2014-12-03 12:42:16