MattM
MattM

Reputation: 817

using sed to search and replace URLs except in certain cases

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.

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:

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

What I have so far with sed is the following:

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

which reformats all the URLs as expected.

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

Thanks in advance for your help!

Upvotes: 2

Views: 199

Answers (2)

rplevy
rplevy

Reputation: 5463

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

Upvotes: 1

William Pursell
William Pursell

Reputation: 212248

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'

Upvotes: 2

Related Questions