繁体   English   中英

Bash变量搜索和替换而不是sed

[英]Bash variable search and replace instead of sed

参见代码审查

参见Github项目

我需要逐行解析+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.

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