#!/bin/sh
masq='(^|[^.0-9])((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])([^\.]|$)'
cat badip2.txt |egrep "$masq" > badip3.txt
Reference: http://www.unix.com/shell-programming-scripting/66386-problem-regexp-ip-adress-pattern.html