[英]Bash variable search and replace instead of sed
我需要逐行解析+word+
實例(用空白替換+word+
)。 我目前正在使用以下(有效的)sed正則表達式:
newLine=$(echo "$line" | sed "s/+[a-Z]\++//g")
根據“ ShellCheck”驗證,這違反了“ SC2001”;
SC2001: See if you can use ${variable//search/replace} instead.
我嘗試了幾種變體而沒有成功(字符串“ + word +”保留在輸出中):
newLine=$(line//+[a-Z]+/)
newLine=$(line/+[a-Z]+//)
newLine=$(line/+[a-Z]\++/)
newLine=${line//+[a-Z]+/}
and more..
我聽說在某些情況下sed是必要的,但我想盡可能使用Bash內置的find和replace方法。
參數擴展中的替換不使用正則表達式,而是使用patterns 。 為了更接近正則表達式,您可以打開擴展模式:
shopt -s extglob
new_line=${line//++([a-Z])+}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.