0

I've a huge list of IP's which is already sorted, but I still need to group them into subnet. For instance:

223.247.184.95
223.247.186.243
223.247.208.16
223.247.209.139
223.84.128.24        
223.84.159.214       *
223.84.159.245       *

The market IP's with the "*" should all be grouped by '223.84.159.*'. There is no database, just this text file with 10.000 entries !

I tested awk and uniq commands, but my results are all not what I want.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Welcome to Stack overflow, could you please use code tags and show more clearly sample input and expected output please. – RavinderSingh13 Dec 10 '17 at 14:09
  • StackOverflow is about helping people fix their existing programming code. Requests for tutorials, research, tools, recommendations, libraries, and code are off-topic. ***Please*** read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Dec 10 '17 at 14:34
  • Your input already **has** the IPs grouped by subnet so it's not clear what you actually want to output that's different from your input. [edit] your question to clarify your requirements and show the expected output given that input. – Ed Morton Dec 10 '17 at 15:55
  • Also, with CIDR, we don't know if a particular (say) class C has been split into smaller subnets. Do you just want the Class A / Class B / Class C subnet divisions? – tripleee Dec 10 '17 at 18:18

2 Answers2

0

It is not clear, as you haven't shown the output. Could you please try following and let me know if this helps you, it will print only those lines which have * in them from Input_file.

awk '$2=="*"{print $1}'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 3
    I strongly suspect the `*`s don't really exist in the input and the OP (unfortunately!) added them to highlight those lines. – Ed Morton Dec 10 '17 at 16:39
0

Say your IP's are in a file called ips spread over different fields over different lines and the field separator is the default one, namely a space.

Example: cat ips

223.247.184.95 223.247.186.243 223.247.208.16 223.247.209.139 223.84.128.24
223.84.159.214* 223.84.159.245*

then for

Unsorted IPs file: the following awk code

cat ips | awk '{for(i=1;i<=NF;i++){split($i,a,".");k=a[1]"."a[2]"."a[3];h[k]=h[k]" "$i}}END{for(k in h)printf(k": "h[k]"\n")}'

gives you a hash table of the different C domains:

223.247.184:  223.247.184.95
223.247.186:  223.247.186.243
223.84.159:  223.84.159.214* 223.84.159.245*
223.84.128:  223.84.128.24
223.247.208:  223.247.208.16
223.247.209:  223.247.209.139

Sorted IPs file: If the file containing your IP has been previously sorted, then there is a more efficient and faster one-liner

cat ips| awk '{for(i=1;i<=NF;i++){split($i,a,".");cn=a[1]"."a[2]"."a[3]; if(cn != c){c=cn;printf("\n"c": ")};printf($i" ")}}END{printf("\n")}'

giving you as well

223.247.184: 223.247.184.95
223.247.186: 223.247.186.243
223.247.208: 223.247.208.16
223.247.209: 223.247.209.139
223.84.128: 223.84.128.24
223.84.159: 223.84.159.214* 223.84.159.245*
MASL
  • 929
  • 1
  • 11
  • 25
  • 2
    Those are [useless uses of `cat`](http://www.iki.fi/era/unix/award.html) – tripleee Dec 10 '17 at 18:17
  • 1
    @tripleee Cheese! When will these fundamentalists of pipe stop annoying around: Those uses of cat are perfectly valid and even elegant: You just need to know when/where (not) to use them. Besides: it's totally off-topic -unless you can prove here that it isn't. – MASL Dec 10 '17 at 18:26
  • 1
    In this individual example, it is obviously fairly harmless, but it continues to be poor advice; how do you know the OP, or a future visitor, doesn't need this to be run inside a tight loop millions of times? – tripleee Dec 10 '17 at 18:27
  • 1
    @tripleee That's exactly my point: The OP and anybody else needs to learn on their own. The problem stated is not about efficiency. Btw, no offence intended in my previous comment. It's just that it has become a trend to criticize this use of cat, **irrespectively of the context**. – MASL Dec 10 '17 at 22:58
  • 1
    [usesful vs useless uses of cat (UvUUC)](https://stackoverflow.com/questions/11710552/useless-use-of-cat?noredirect=1&lq=1) – MASL Dec 11 '17 at 00:06
  • I'm obviously well aware of that question, but your link prompted me to post an answer of my own. – tripleee Dec 11 '17 at 05:29