2013-06-27 180 views
0

標籤的位置需要位於標籤之前。有500個我需要排序的實例。在<a href>標籤前移動<a id>標籤

例如:

<h2 class="chapter-title"><a href="contents.html#Endnotes"><a id="Anchor"/>Endnotes and References</a></h2> 

我尋找

(<a [^>]*href\s*=\s*")([^"]+)("[^>]*>)(<a [^>]*id\s*=\s*")([^"]+)("[^>]*>) 

正確地拿起兩個標籤,但我在努力尋找合適替代糾正位置。

幫助!! ???

回答

2

這個工作對我說:

$ sed 's#\(<a href[^>]*>\)\(<a id[^>]*>\)#\2\1#g' b 
<h2 class="chapter-title"><a id="Anchor"/><a href="contents.html#Endnotes">Endnotes and References</a></h2> 

我們期待文本<a href[^>],並將其保存在第一變種。然後我們尋找文本<a id[^>]並保存在第二個變種中。最後我們交換它們。

注意<a id[^>]意思是「一切從<a id和高達>字符開始。

0

我這個這個應該工作。你只需要兩個捕獲組,一組爲<a href...>,另一個用於<a id...>

sed 's#\(<a [^>]*href\s*=\s*"[^"]+"[^>]*>\)\(<a [^>]*id\s*=\s*"[^"]+"[^>]*>\)#\2\1#' 
0

你可以試試這個,我認爲這應該工作

<a id="Anchor" href="contents.html#Endnotes">EndNotes and Refrences</a>