aboutsummaryrefslogtreecommitdiff
path: root/challenge-112/abigail/awk/ch-1.awk
blob: fde01cf5de784b5e16f7094281ccfaad19ec0143 (plain)
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
43
44
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-1.awk < input-file
#

BEGIN {
    FS = "/"   # So we split into directory parts
}

{
    delete path
    j = 0                            # Tracks the number of parts in
                                     # the canonical part.
    for (i = 1; i <= NF; i ++) {     # Loop over directory parts
        if ($i == "") {              # Skip empty parts
            continue;
        }
        if ($i == ".") {             # Skip current directory
            continue;
        }
        if ($i == "..") {            # Back up to parent directory
            if (j > 0) {
                j --
            }
            continue;
        }
        path [j] = $i                # Copy
        j ++
    }
    if (j == 0) {                    # Root directory
        print "/"
    }
    else {                           # Print parts, preceeded by a /
        for (k = 0; k < j; k ++) {
            printf "/%s", path [k]
        }
        print ""
    }
}