使用sed搜索和替换url(在某些情况下除外)

时间:2022-08-29 16:49:11

I have a list of urls where, for the majority, I want to do a simple search and replace, but in some cases I want to exclude using sed.

我有一个url列表,对于大多数人来说,我想做一个简单的搜索和替换,但是在某些情况下,我想排除使用sed。

Given the list below:

给定下面的列表:

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

I want to convert all URLs that do not have "sandbox" in the URL to:

我想将URL中没有“沙箱”的所有URL转换为:

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

What I have so far with sed is the following:

到目前为止,我对sed的了解如下:

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

which reformats all the URLs as expected.

按照预期重新格式化所有的url。

How do I modify what I have to exclude the lines that have "sandbox" in them?

我如何修改我必须排除的行,其中有“沙箱”?

Thanks in advance for your help!

谢谢你的帮助!

2 个解决方案

#1


2  

If by exclude, you mean "do not do the replacement", then:

如果您的意思是“不做替换”,则:

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

If you mean 'omit completely from the output':

如果你的意思是“完全忽略输出”:

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

#2


1  

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

#1


2  

If by exclude, you mean "do not do the replacement", then:

如果您的意思是“不做替换”,则:

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

If you mean 'omit completely from the output':

如果你的意思是“完全忽略输出”:

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

#2


1  

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