BASH脚本 - 查找替换多个值

时间:2014-12-03 12:35:56

标签: bash replace find

我有一个非常简单的bash脚本,我将值传递给

我想从传递给脚本的值中去除前缀​​。

工作并从传递的值中剥离test- ..

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

所以test-12345返回12345

是否有任何修改方法,以便删除test-local-

我试过了:

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

但那不起作用..

由于

4 个答案:

答案 0 :(得分:1)

如果您想将“test-”或“local-”更改为“”,您可以使用如下命令:

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

答案 1 :(得分:1)

您可以使用sed并获得准确的结果

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

答案 2 :(得分:1)

尝试使用如下的sed:

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

这里sed会搜索“test-”或“local-”,并在整个输入的任何地方完全删除它们。

答案 3 :(得分: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