Project

General

Profile

Gawk » History » Version 1

Charles Atkinson, 30/04/2020 23:51

1 1 Charles Atkinson
h1. gawk
2 1 Charles Atkinson
3 1 Charles Atkinson
{{toc}}
4 1 Charles Atkinson
5 1 Charles Atkinson
h1. Sorting IPv4 addresses
6 1 Charles Atkinson
7 1 Charles Atkinson
h2. Function cmp_ip_addr
8 1 Charles Atkinson
9 1 Charles Atkinson
<pre>
10 1 Charles Atkinson
# cmp_ip_addr
11 1 Charles Atkinson
# For asort() to call
12 1 Charles Atkinson
# Compares IP addresses in values v1 and v1
13 1 Charles Atkinson
function cmp_ip_addr (i1, v1, i2, v2, \
14 1 Charles Atkinson
    v1_octets, v2_octets)
15 1 Charles Atkinson
{
16 1 Charles Atkinson
    #print "i1: " i1 ", v1: " v1 ", i2: " i2 ", v2: " v2
17 1 Charles Atkinson
    split(v1, v1_octets, ".") 
18 1 Charles Atkinson
    split(v2, v2_octets, ".") 
19 1 Charles Atkinson
20 1 Charles Atkinson
    #print "v1_octets[4]: " v1_octets[4] ", v2_octets[4]: " v2_octets[4]
21 1 Charles Atkinson
    if (v1_octets[1]+0 < v2_octets[1]+0)
22 1 Charles Atkinson
        return -1
23 1 Charles Atkinson
    else if (v1_octets[1]+0 == v2_octets[1]+0) {
24 1 Charles Atkinson
        if (v1_octets[2]+0 < v2_octets[2]+0)    
25 1 Charles Atkinson
            return -1
26 1 Charles Atkinson
        else if (v1_octets[2]+0 == v2_octets[2]+0) {
27 1 Charles Atkinson
            if (v1_octets[3]+0 < v2_octets[3]+0)
28 1 Charles Atkinson
                return -1
29 1 Charles Atkinson
            else if (v1_octets[3]+0 == v2_octets[3]+0) {
30 1 Charles Atkinson
                if (v1_octets[4]+0 < v2_octets[4]+0)
31 1 Charles Atkinson
                    return -1
32 1 Charles Atkinson
                else if (v1_octets[4]+0 == v2_octets[4]+0) {
33 1 Charles Atkinson
                    return 0
34 1 Charles Atkinson
                }   
35 1 Charles Atkinson
                else
36 1 Charles Atkinson
                    return 1
37 1 Charles Atkinson
            }   
38 1 Charles Atkinson
            else
39 1 Charles Atkinson
                return 1
40 1 Charles Atkinson
        }   
41 1 Charles Atkinson
        else
42 1 Charles Atkinson
            return 1
43 1 Charles Atkinson
    }   
44 1 Charles Atkinson
    else
45 1 Charles Atkinson
        return 1
46 1 Charles Atkinson
}
47 1 Charles Atkinson
</pre>
48 1 Charles Atkinson
49 1 Charles Atkinson
h2. Usage example
50 1 Charles Atkinson
51 1 Charles Atkinson
Addresses is an array with (FQDNs as indices and) IP addresses as values.  Array networks is derived containing in ascending order all the unique /24 networks corresponding to the IP addresses from array Addresses.
52 1 Charles Atkinson
<pre>
53 1 Charles Atkinson
    # Create array of indices to Addresses in address order
54 1 Charles Atkinson
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 1 Charles Atkinson
    addresses_n = asort(Addresses, addresses_sorted, "cmp_ip_addr"))
56 1 Charles Atkinson
57 1 Charles Atkinson
    # Create sorted list of /24 networks
58 1 Charles Atkinson
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 1 Charles Atkinson
    for (i = 1; i <= addresses_n; i++)
60 1 Charles Atkinson
    {
61 1 Charles Atkinson
        address = addresses_sorted[i]
62 1 Charles Atkinson
        network = gensub(/\.[^.]+$/, "", 1, address)
63 1 Charles Atkinson
        networks[network] = ""
64 1 Charles Atkinson
    }
65 1 Charles Atkinson
</pre>