Skip to main content

Playing around with awk

My first little awk script for fun: linenumbers

#!/usr/bin/awk -f
#
# Copyright (C) 2009 Sebastian Pipping
# Released under GPLv3
#

#
# Usage:
#   ./linenumbers <file1> <file2> ...
# or
#   ... | ./linenumbers
#

BEGIN {
    firstFile = 1
    fileName = "-"
}

{
    if (fileName != FILENAME) {
        fileName = FILENAME

        if (!firstFile) {
            print ""
        } else {
            firstFile = 0
        }

        printf "## File \"%s\"\n", FILENAME
        NR = 1
    }

    printf "%05d %s\n", NR, $0
}