我在一个非常大的文档中有以下字符串:
1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#
Run Code Online (Sandbox Code Playgroud)
现在我想.md#
用.html#
但仅当http
字符串中没有时替换每个。所以只有 2 和 6 应该有一个替代品。如何在 shell 脚本中执行此操作?
用 GNU sed。如果当前行(模式空间)包含http
跳转到脚本结尾 ( b
)。否则进行搜索和替换。
sed '/http/b; s/\.md#/.html#/' file
Run Code Online (Sandbox Code Playgroud)
输出:
1.test.html# 2.test.html# 3.http://test.html# 4.https://test.md# 5.http://test.md# 6.test2.html#
如果要“就地”编辑文件,请使用 sed 的 option -i
。
看: man sed