【发布时间】:2017-10-09 14:11:45
【问题描述】:
我一直在尝试进行 sed 替换。出于某种原因,我无法理解 sed 和正则表达式(这是我的第二个 sed 问题)
基本上我想查找文本并替换,然后找到下一次出现的某些文本并替换它
到目前为止我有这个
echo "This could be anywhere and this bit should be changed \$\{hcvar-BookName\} and this would remain" | sed 's/$\\{hcvar-/" + hcvar-/g'
这会返回:
*This could be anywhere and this bit should be changed " + hcvar-BookName\} and this would remain*
但是我想返回
*This could be anywhere and this bit should be changed " + hcvar-BookName + " and this would remain*
这会将 } 替换为 + "
逻辑是这样的:
Find: \$\{hcvar-
Replace with: " + hcvar-
Then find the next occurrence of: \}
Replace with: + "
要替换的第二位将紧跟在包含以下内容的字符串之后:hcvar-
这应该适用于以下字符串示例
- 这可能在任何地方,应该更改此位 \${hcvar-BookName} 这将保留
- 这可能在任何地方,并且 应该更改此位 \${hcvar-somethingelse} 这将 留下
- 这可能在任何地方,应该更改此位
\${hcvar-wouldnevercontainspaces} 这将保留
任何帮助将不胜感激。
【问题讨论】:
标签: sed