2

We are starting a new proxy server at work and have a list of domains that are to be blocked; the list is downloaded from an external site.

To place them in Squid format we have to add a . to the beginning of the line which we currently have setup simply /bin/sed -i 's/^/./' $file

The problem we have come across is that some of the files include IP addresses (which we would not like to add a leading . to)

I’ve been playing around with sed more and think this should work but it doesn’t:

sed '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/s/^\.//g'

Can someone either suggest how I can remove the leading . from IP addresses, or even modify the first command to not add the . to IP addresses?

Here is a list of sample data domain names after the . has been added to the beginning of every line:

.216.32.68.172
.216.32.68.173
.216.32.68.188
.216.32.90.27
.216.32.90.28
.216.32.94.117
.21ace.com
.21publish.com
.21wuxia.com
.222z.net
.22dakika.org
.23-net.org
.23zone.de
.244.120.232.72.static.reverse.ltdomains.com
.24hourcafe.biz.ly
0

1 Answer 1

2

You were very close, just escape the {}-characters.

This will put a . in front of all patterns that don't match a ip address:

sed '/^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/! s/^/./g' file

This will remove all leading . in front of ip adresses:

sed '/^.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/ s/^.//g' file2
1
  • Great, thanks for that... It was working with egrep, didn't know I had to escape more with sed.
    – Andrew
    Mar 11, 2015 at 0:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .