Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在bash中用另一个字符串替换可变长度的重复字符串?_Regex_Bash_Replace - Fatal编程技术网

Regex 如何在bash中用另一个字符串替换可变长度的重复字符串?

Regex 如何在bash中用另一个字符串替换可变长度的重复字符串?,regex,bash,replace,Regex,Bash,Replace,我有一些文件,其中缺少的数据以“+”的形式插入。所以线条看起来像这样: substring1+++++substring2++++++++++++++substring3+substring4 我想用“缺失”替换所有重复的“+”>5。这使我的团队更易于阅读,并且更容易看到缺失数据和输入为“+”的数据之间的差异(最多允许5个)。 到目前为止,我已经: while read l; do echo "${l//['([+])\1{5}']/'MISSING'}" done < /path/

我有一些文件,其中缺少的数据以“+”的形式插入。所以线条看起来像这样:

substring1+++++substring2++++++++++++++substring3+substring4
我想用“缺失”替换所有重复的“+”>5。这使我的团队更易于阅读,并且更容易看到缺失数据和输入为“+”的数据之间的差异(最多允许5个)。 到目前为止,我已经:

while read l; do
  echo "${l//['([+])\1{5}']/'MISSING'}"
done < /path/file.txt
读l时
;做
回显“${l/['([+])\1{5}]/'MISSING'}”
完成
但这会将每个“+”替换为“缺少”。我只需要说一次“失踪”


提前感谢。

在Bash变量扩展中不能使用正则表达式

在循环中,可以使用

sed 's/+\{1,\}/MISSING/g' <<< "$l"
+\{1,\}
POSIX BRE模式匹配文本
+
+
)1次或多次(
\{1,\}


sed的/+\{1,\}/MISSING/g'不能在Bash变量扩展中使用正则表达式。使用sed的/+\{1,\}/MISSING/g'Hi!你选择的答案没有任何补充,它是完美的。无论如何,我认为这对您可能有用。对于在
行中对文件进行更改-I
可以used@DigvijayS这取决于操作系统和sed版本。有更多的解决方案。
sed 's/+\{1,\}/MISSING/g' /path/file.txt
sed 's/+\{1,\}/MISSING/g' <<< "substring1+++++substring2++++++++++++++substring3+substring4"
# => substring1MISSINGsubstring2MISSINGsubstring3MISSINGsubstring4