2012-10-09 35 views
2

我有一個網址列表,在大多數情況下,我想做一個簡單的搜索和替換,但在某些情況下,我想排除使用sed。使用sed來搜索和替換網址,除了在某些情況下

給出下面的列表:

http://www.dol.gov 
http://www.science.gov 
http://www.whitehouse.gov 
http://test.sandbox.local 
http://www.travel.state.gov 
http://www.lib.berkeley.edu 
http://dev.sandbox.local 

我想沒有「沙箱」中的URL所有URL轉換:

href="/fetch?domain=<url>" 

我至今與戰略經濟對話的以下內容:

sed -r 's|http://(\S*)|href="/fetch\?domain=\1"|g' 

根據預期重新格式化所有URL。

如何修改我必須排除的內容中包含「沙盒」的行?

在此先感謝您的幫助!

回答

2

如果排除,你的意思是 「不做更換」,則:

sed -r '/sandbox/!s|http://(\S*)|href="/fetch\?domain=\1"|g' 

如果你的意思是 '從輸出完全省略':

sed -r -e '/sandbox/d' -e 's|http://(\S*)|href="/fetch\?domain=\1"|g' 
1
sed -r 's|http://(\S*)|href="/fetch\?domain=\1"|g' | grep -v sandbox