2012-04-09 25 views
0

如何搜索基於字符串匹配的特定行並將其替換爲另一個sting。 下面是一個html強壯的例子,我需要使用bash腳本進行修改。如何搜索和替換HTML文件中的標記文本,而不是行號

< link type="text/css" rel="stylesheet" href="https://teststore.xxx.com/store/downpanel.css"> 

變化:

< link type="text/css" rel="stylesheet" href="https://testsstore.xxx.com/store/downpanel.css"> 

即teststore與testsstore。只是想添加's'。

我想我需要匹配所有的字符串。因爲downpanel.css是區分要用's'編輯的行。

我被說這可以通過Regualar表達式實現..但我無法做到。任何語法幫助都將非常有用。

謝謝。
插孔

+2

Ob http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454:不要使用正則表達式來匹配HTML。 – geekosaur 2012-04-09 10:22:34

回答

1

如果您需要更換所有出現這一環節,只是做

sed 's_"https://teststore.xxx.com/store/downpanel.css"_"https://testsstore.xxx.com/store/downpanel.css"_g' old_file > new_file 

如果你真的需要向您展示整個零件匹配,然後把在sed命令。謹防換行符,如果遇到中間某處,他們會破壞匹配。

這是sedreference。或者在Linux上鍵入man sed

+0

感謝你們所有人.. ..'s_「https://teststore.xxx.com/store/downpanel.css」_「https://testsstore.xxx.com/store/downpanel.css」_g'did當我試着工作。 – Johnbritto 2012-04-09 12:08:39

+0

@Johnbritto:不要忘記接受幫助你的答案。請參閱http://stackoverflow.com/faq#howtoask – Johnsyweb 2012-05-14 11:55:22

0

爲了解決類似這樣的問題,通常可使用一個sed構建體是這樣的:

sed -e '/unique pattern/s/string or pattern/replacement string/' file 

初始/unique pattern/部分限制的取代到的圖案給定行。對於簡單的情況下,像你這樣的,它可能是足夠簡單地執行對包含teststore所有線路的替代,從而獨特的圖案部分可以省略,替代成爲全球:

sed -e 's/teststore/testsstore/' file 

如果這是你的問題並替換它不應該使用原始形式的東西,可能是這樣的:

sed -e '/link.*teststore/s/teststore/testsstore/' file 

爲了幫助限制影響。

請注意,這隻會將修改後的版本file寫入標準輸出。要進行更改,請將-i切換到sed

相關問題