File: rewrite-peer-groups.awk

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (42 lines) | stat: -rwxr-xr-x 1,453 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/awk -f
# This awk program rewrites peer group IDs, as described by
# proc_pid_mountinfo(5), into sequential values starting from the magic value
# 42.

BEGIN {
    # Start with 42 as a recognizable group number.
    group = 42
    # Conveniently both "shared" and "master" have the same length.
    # This also makes the match below easier as there's only one regular
    # expression and one loop to work with.
    prefix1_len = length("shared:")
    # The second prefix is for the less commonly seen "propagate_from" field.
    prefix2_len = length("propagate_from:")
}

{
    # Starting with field 7, which is the first optional field, rewrite
    # subsequent fields until we reach the end all fields or until we see the
    # optional field list terminator, dash.
    for (n = 7; n < NF && $(n) != "-"; n++) {
        # Find the NNN number of the peer group ID.
        if (match($(n), /(shared|master):[0-9]+/)) {
            id = substr($(n), RSTART + prefix1_len)
        } else if (match($(n), /propagate_from:[0-9]+/)) {
            id = substr($(n), RSTART + prefix2_len)
        } else {
            continue
        }

        # If we have not seen this number yet, assign the next replacement ID.
        if (!(id in seen)) {
            seen[id] = group++
        }

        # Replace the actual peer group ID with the replacement ID.
        sub(/[0-9]+/, seen[id], $(n))
    }

    # Print each rewritten record.
    print
}