A quine program written in awk

# quine.awk; does nothing but print itself out.
# awk -f quine.awk > xxx; diff quine.awk xxx

# print indented assignment statement with rhs in quotes
function print_asg(name, str) { print sp name eq qt str qt }

BEGIN {
    # tricks to avoid quote marks outside assignment block
    b = "b"
    e = "e"
    p = "p"
    q = "q"
    s = "s"
    t = "t"
    sp = "    "
    eq = " = "
    qt = "\""
    bs = "\\"
    tt = "top[++tc]"
    bb = "bot[++bc]"

    top[++tc] = "# quine.awk; does nothing but print itself out."
    top[++tc] = "# awk -f quine.awk > xxx; diff quine.awk xxx"
    top[++tc] = ""
    top[++tc] = "# print indented assignment statement with rhs in quotes"
    top[++tc] = "function print_asg(name, str) { print sp name eq qt str qt }"
    top[++tc] = ""
    top[++tc] = "BEGIN {"
    top[++tc] = "    # tricks to avoid quote marks outside assignment block"

    bot[++bc] = ""
    bot[++bc] = "    # print first lines of source file"
    bot[++bc] = "    for (i = 1; i <= tc; i++)"
    bot[++bc] = "        print top[i];"
    bot[++bc] = ""
    bot[++bc] = "    # print tricky individual assignments"
    bot[++bc] = "    print_asg(b, b)"
    bot[++bc] = "    print_asg(e, e)"
    bot[++bc] = "    print_asg(p, p)"
    bot[++bc] = "    print_asg(q, q)"
    bot[++bc] = "    print_asg(s, s)"
    bot[++bc] = "    print_asg(t, t)"
    bot[++bc] = "    print_asg(s p, sp)"
    bot[++bc] = "    print_asg(e q, eq)"
    bot[++bc] = "    print_asg(q t, bs qt)"
    bot[++bc] = "    print_asg(b s, bs bs)"
    bot[++bc] = "    print_asg(t t, tt)"
    bot[++bc] = "    print_asg(b b, bb)"
    bot[++bc] = ""
    bot[++bc] = "    print"
    bot[++bc] = ""
    bot[++bc] = "    # print assignment stmts for first lines of source file"
    bot[++bc] = "    for (i = 1; i <= tc; i++)"
    bot[++bc] = "        print_asg(tt, top[i]);"
    bot[++bc] = ""
    bot[++bc] = "    print"
    bot[++bc] = ""
    bot[++bc] = "    # print assignment stmts for last lines of source file"
    bot[++bc] = "    for (i = 1; i <= bc; i++)"
    bot[++bc] = "        print_asg(bb, bot[i]);"
    bot[++bc] = ""
    bot[++bc] = "    # print last lines of source file"
    bot[++bc] = "    for (i = 1; i <= bc; i++)"
    bot[++bc] = "        print bot[i];"
    bot[++bc] = "}"

    # print first lines of source file
    for (i = 1; i <= tc; i++)
        print top[i];

    # print tricky individual assignments
    print_asg(b, b)
    print_asg(e, e)
    print_asg(p, p)
    print_asg(q, q)
    print_asg(s, s)
    print_asg(t, t)
    print_asg(s p, sp)
    print_asg(e q, eq)
    print_asg(q t, bs qt)
    print_asg(b s, bs bs)
    print_asg(t t, tt)
    print_asg(b b, bb)

    print

    # print assignment stmts for first lines of source file
    for (i = 1; i <= tc; i++)
        print_asg(tt, top[i]);

    print

    # print assignment stmts for last lines of source file
    for (i = 1; i <= bc; i++)
        print_asg(bb, bot[i]);

    # print last lines of source file
    for (i = 1; i <= bc; i++)
        print bot[i];
}