我正在做一些維基/降價的轉換,並有串這樣的:如何使用sed替換一些帶連字符的空格?
[[Moo Cow]]
,我希望它是這樣的:
[Moo Cow](Moo-Cow.html)
我怎樣才能做到這一點使用sed
?
我正在做一些維基/降價的轉換,並有串這樣的:如何使用sed替換一些帶連字符的空格?
[[Moo Cow]]
,我希望它是這樣的:
[Moo Cow](Moo-Cow.html)
我怎樣才能做到這一點使用sed
?
這是一個sed
命令,將做到這一點對你:
sed -i 's/\]\]/\]\(\.html\)/g; s/\[\[/\[/g; s/\[\(.*\)\](\.html)/\[\1\](\1.html)/g; :a;s/\(([^)]*\) /\1-/;ta' filename.md
不,那是不漂亮。是的,這很醜陋。但是,它的工作。
它可以與GNU'sed'一起工作,即使對於[[[multiple words here]]中的多個單詞以及多個外部單詞也是如此。但是,如果線上有多個'[[Magic Pattern]]',就會出現問題。 –
可以減少你的sed表達這樣的:
sed -i 's/\[\[\([^]]*\)]]/[\1](\1.html)/g;:a;s/\(]([^) ]*\) /\1-/g;ta;' file
或用Perl:
perl -pe's/\[\[([^]]*)]]/$a=$1;$a=~y# #-#;"[$1]($a.html)"/ge' file
只需使用AWK:
$ awk '{gsub(/[][]/,""); x=$0; gsub(/ /,"-"); printf "[%s](%s).html\n", x, $0}' file
[Moo Cow](Moo-Cow).html
你不打算做多用連字符替換空格? – chris85
可能會超過2個單詞,比如'[[Some Foo Bar Cow]]'? – RomanPerekhrest