2014-12-03 123 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