简体   繁体   English

BASH脚本-查找替换多个值

[英]BASH Script - Find Replace multiple values

I've got a very simple bash script which I'm passing values to 我有一个非常简单的bash脚本,正在将值传递给

I want to strip the prefix from the value passed to the script. 我想从传递给脚本的值中删除前缀。

The works and strips test- from the passed value.. 从传递的值中提取并test-

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

So test-12345 returns 12345 所以test-12345返回12345

Is there anyway to amend this so it will remove either test- or local- ? 无论如何,有什么要修改的,以便删除test-local-

I've tried : 我试过了 :

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

But that didn't work.. 但这没用..

Thanks 谢谢

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

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

You can use sed and get the exact result 您可以使用sed并获得准确的结果

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

Try using sed as below: 尝试如下使用sed:

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

Here sed will search for "test-" or "local-" and remove them completely anywhere in the whole input. sed在这里将搜索“ test-”或“ local-”,并将其完全删除。

You can do it with extglob activated: 您可以在激活extglob下执行此extglob

shopt -s extglob
arrIN=(${IN//+(test-|local-)/})

From man bash : 来自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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM