I want to remove all the IP addresses which are of the format 10.20.30.* and whose last 3 digits fall in the range 0 to 200 and move all the remaining IPs to a new file.

I've put the IP list to be excluded in the file whiteIP.txt and its content are:


And my original file's content is:,20,20,20,20,20,20,20,20,20

I'm trying the command:

grep -vE -f whitetest.txt testIP.txt

It's giving me wrong result. It should give me the following result:,20,20,20,20,20

Any suggestions would be of great help.


Several things:

. is a special character, therefore it has to be escaped:


2[00]matches 20, not 200:


You have to handle single-digit and double-digit numbers separately:


This gives the correct result:

$ grep -vE -f whitetest.txt testIP.txt 20 20 20 20 20
  • Thanks for this. It is working perfectly. I also want to exclude all the IPs that are starting with 10. I've written something like below in whitetest.txt. ^10\.([0-9][0-9][0-9]|[0-9][^0-9]|[0-9][0-9][^0-9]).([0-9][0-9][0-9]|[0-9][^0-9]|[0-9][0-9][^0-9]).([0-9][0-9][0-9]|[0-9][^0-9]|[0-9][0-9][^0-9]) But it is giving me this result:,20,20,20,20 Leaving behind,20 line. It should not be the case. Please help. – prashant1988 Mar 11 '15 at 12:27
  • This is working perfectly for me - ^10.([0-9]*).([0-9]*).([0-9]*) Just wondering as to why my last entry wasn't working good. – prashant1988 Mar 11 '15 at 15:01
  • I've edited my questions with some additional query. Please help. – prashant1988 Mar 17 '15 at 6:54
  • @prashant1988 please don't do that. When asking here, each question should be specific and localized. Since this solved your issue, please accept it and post your follow-up query as a separate question. – terdon Mar 17 '15 at 13:33

This should get what you want, I think:

grep -vE '10\.20\.30\.(1|..?,|200)
' <<\IN,20,20,20,20,20,20,20,20,20

Basically you just have to exclude any match for 10.20.30 with a final octet fewer than three characters, that begins with 1, or that is wholly 200.

The above prints:,20,20,20,20,20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.