I'm doing some wiki/markdown transformations, and have string like this:
我正在做一些wiki/markdown转换,有这样的字符串:
[[Moo Cow]]
and I want it to be like this:
我希望它是这样的:
[Moo Cow](Moo-Cow.html)
How can I do this using sed
?
如何使用sed进行此操作?
3 个解决方案
#1
2
You can reduce your sed expression like this:
您可以这样减少您的sed表达式:
sed -i 's/\[\[\([^]]*\)]]/[\1](\1.html)/g;:a;s/\(]([^) ]*\) /\1-/g;ta;' file
or with perl:
或用perl:
perl -pe's/\[\[([^]]*)]]/$a=$1;$a=~y# #-#;"[$1]($a.html)"/ge' file
#2
0
Just use awk:
只使用awk:
$ awk '{gsub(/[][]/,""); x=$0; gsub(/ /,"-"); printf "[%s](%s).html\n", x, $0}' file
[Moo Cow](Moo-Cow).html
#3
0
This is a sed
command that will do that for you:
这是一个sed命令,将为您做到这一点:
sed -i 's/\]\]/\]\(\.html\)/g; s/\[\[/\[/g; s/\[\(.*\)\](\.html)/\[\1\](\1.html)/g; :a;s/\(([^)]*\) /\1-/;ta' filename.md
No, that is not pretty. Yes, that is ugly. But, it works.
不,那不漂亮。是的,这是丑陋的。但是,它的工作原理。
#1
2
You can reduce your sed expression like this:
您可以这样减少您的sed表达式:
sed -i 's/\[\[\([^]]*\)]]/[\1](\1.html)/g;:a;s/\(]([^) ]*\) /\1-/g;ta;' file
or with perl:
或用perl:
perl -pe's/\[\[([^]]*)]]/$a=$1;$a=~y# #-#;"[$1]($a.html)"/ge' file
#2
0
Just use awk:
只使用awk:
$ awk '{gsub(/[][]/,""); x=$0; gsub(/ /,"-"); printf "[%s](%s).html\n", x, $0}' file
[Moo Cow](Moo-Cow).html
#3
0
This is a sed
command that will do that for you:
这是一个sed命令,将为您做到这一点:
sed -i 's/\]\]/\]\(\.html\)/g; s/\[\[/\[/g; s/\[\(.*\)\](\.html)/\[\1\](\1.html)/g; :a;s/\(([^)]*\) /\1-/;ta' filename.md
No, that is not pretty. Yes, that is ugly. But, it works.
不,那不漂亮。是的,这是丑陋的。但是,它的工作原理。