aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/abigail/awk/ch-1.awk
blob: ac53032a93129a49e43beb241a787aea71bbd9ab (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
#!/usr/bin/awk

#
# See ../README.md
#

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


#
# If we haven't ready in the content of "filename" yet, do so,
# and store it into an array, indexed by the filename.
#
# Remove the first amount characters of the stored content, and
# return it.
#
function readN(filename, amount) {
    if (!content [filename]) {
        old_rs = RS
        #
        # We want to read the entire file, so we pick a
        # character which should not be present in the file
        # as the record separator.
        #
        RS = "\000"
        getline content [filename] < filename
        #
        # Restore the record separator.
        #
        RS = old_rs
    }
    r = substr (content [filename], 1, amount)
    content [filename] = substr (content [filename], amount + 1)
    return r
}

{
    print readN($1, $2)
}