我有一个 .md
文件,其中包含对其他 .md
文件的多个引用,其形式为:
[Another file](dir/file.md)
[And another file](dir2/anotherfile.md)
[Yet another file](dir3/yetanotherfile.md)
适合此内联替换的 sed
命令是什么:
[Another file](pdf/dir1/file.pdf)
[And another file](pdf/dir2/anotherfile.pdf)
[Yet another file](pdf/dir3/yetanotherfile.pdf)
我知道如何捕捉具有特定模式的线条,例如通过
sed -rn '/\(.+\.md\)/p' README.md
但我无法对捕获组进行适当的操作来执行转换。
谢谢。
最佳答案
尝试以下操作:
sed -re 's#\[([^]]+)\]\(([^\)]+)\.md\)#[\1](pdf/\2.pdf)#g'
这将匹配整个链接表达式,并将 *.md
替换为 pdf/*.pdf
。这也将匹配一行中某处的链接 - 而不是单独的。
如果您想更新文件的内容,请使用 -i
标志:
sed -re 's#\[([^]]+)\]\(([^\)]+)\.md\)#[\1](pdf/\2.pdf)#g' -i README.md
输入:
[Another file](dir/file.md)
[And another file](dir/anotherfile.md)
[Yet another file](dir/yetanotherfile.md)
输出:
[Another file](pdf/dir/file.pdf)
[And another file](pdf/dir/anotherfile.pdf)
[Yet another file](pdf/dir/yetanotherfile.pdf)
关于bash - 使用 sed 内联替换部分匹配行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888922/