簡體   English   中英

BASH腳本-查找替換多個值

[英]BASH Script - Find Replace multiple values

我有一個非常簡單的bash腳本,正在將值傳遞給

我想從傳遞給腳本的值中刪除前綴。

從傳遞的值中提取並test-

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

所以test-12345返回12345

無論如何,有什么要修改的,以便刪除test-local-

我試過了 :

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

但這沒用..

謝謝

如果要將“ test-”或“ local-”更改為“”,則可以使用以下命令:

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

您可以使用sed並獲得准確的結果

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

嘗試如下使用sed:

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

sed在這里將搜索“ test-”或“ local-”,並將其完全刪除。

您可以在激活extglob下執行此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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM