#! /usr/bin/gawk -f # Last edited on 2004-02-07 04:56:45 by stolfi BEGIN { # Lists occurrences of a pattern with context if (pat == "") { arg_error("must specify \"pat\""); } if (sep == "") { sep = "|"; } } /^[ ]*([\#@]|$)/ { next; } /./ { lin = $0; # Replace inlined @{}{}-constructs by their text content: lin = gensub(/[@][{][^{}]*[}][{]([^{}]*)[}]/, "\\1", "g", lin); # Remove {}-comments: gsub(/[{][^{}]*[}]/, "", lin); # Continue processing: $0 = lin; } (match($0, pat)) { lin = $0; a = substr(lin, 1, RSTART-1); b = substr(lin, RSTART, RLENGTH); c = substr(lin, RSTART+RLENGTH); nb = length(b); wa = int((80 - nb)/2); wc = 80 - wa - nb; printf "%*s%s%s%s%s\n", wa, substr(a, length(a)+1-wa), sep, b, sep, substr(c, 1, wc); next; } function arg_error(msg) { printf "%d: %s\n", FNR, msg > "/dev/stderr"; exit 1; }