Use grep instead egrep.
Use grep -v 'nameserver\s\s*127.' instead of grep -P ...
Use head -1 instead head -3