0

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:

^10.20.30.([0-1][0-9][0-9]|2[00])

And my original file's content is:

10.20.30.150,20
10.20.30.134,20
10.20.30.201,20
10.20.30.5,20
10.20.30.250,20
10.20.30.42,20
222.233.201.5,20
10.233.201.5,20
111.233.201.5,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:

10.20.30.201,20
10.20.30.250,20
222.233.201.5,20
10.233.201.5,20
111.233.201.5,20

Any suggestions would be of great help.

1

Several things:

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

^10\.20\.30\.([0-1][0-9][0-9]|2[00])

2[00]matches 20, not 200:

^10\.20\.30\.([0-1][0-9][0-9]|200)

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

^10\.20\.30\.([0-1][0-9][0-9]|200|[0-9][^0-9]|[0-9][0-9][^0-9])

This gives the correct result:

$ grep -vE -f whitetest.txt testIP.txt
10.20.30.201 20
10.20.30.250 20
222.233.201.5 20
10.233.201.5 20
111.233.201.5 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: 10.20.30.5,20 67.221.34.222,20 222.233.201.5,20 111.233.201.5,20 Leaving behind 10.20.30.5,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
0

This should get what you want, I think:

grep -vE '10\.20\.30\.(1|..?,|200)
' <<\IN
10.20.30.150,20
10.20.30.134,20
10.20.30.201,20
10.20.30.5,20
10.20.30.250,20
10.20.30.42,20
222.233.201.5,20
10.233.201.5,20
111.233.201.5,20
IN

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:

10.20.30.201,20
10.20.30.250,20
222.233.201.5,20
10.233.201.5,20
111.233.201.5,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.