2014-12-03 109 views
1

我有我正經過值bash腳本 - 查找替換多個值

我要剝去傳遞給腳本值前綴一個非常簡單的bash腳本。

從傳遞價值的作品和帶test- ..

IN=$1 
arrIN=(${IN//test-/}) 
echo $arrIN 

所以測試12345 12345返回

反正是有修改這一所以它會刪除或者test-local-

我已經試過:

arrIN=(${IN//test-|local-/}) 

但是,這並不會工作。

感謝

+0

'$ {VAR ## * - }'從去年得到部分'-'來字符串的結尾。但是你正在使用數組符號,所以目前還不清楚這是否足夠/ – fedorqui 2014-12-03 12:42:16

回答

1

嘗試使用SED如下:

IN=$1 
arrIN=$(echo $IN | sed -r 's/test-|local-//g') 
echo $arrIN 

這裏的sed將搜索「測試 - 」或「局地」,並在整個輸入任何地方完全刪除它們。

+0

謝謝 - 我已經去了這個答案,因爲我的理解更容易.. 當我回顧這6個月,我會知道什麼它確實:) – Rocket 2014-12-03 12:57:36

+0

我編輯過這個帖子來解釋sed在那裏做什麼。 – SMA 2014-12-03 13:01:46

1

如果你想改變 「測試 - 」 或 「局地」 到 「」 ,你可以使用如下命令:

awk '{gsub(/test-|local-/, ""); print}' 
1

您可以使用sed,並得到確切的結果

IN=$1 
arrIN=$(echo $IN | sed 's/[^-]\+.//') 
echo $arrIN 
1

你可以用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