[Previous] [TOC] [Next]

Awk: Example for a filter

Aim: Add (DSC) page numbers to a Postscript document.

Example Code

# Filter Example adding DSC comments
BEGIN { page = 0 }
{if ($0 ~/^%!PS-Adobe/) page++}
{if (toupper($0) !~ /^%%PAGE:/) 
     print $0}
{if (toupper($0)~/^%%ENDCOMMENTS/) 
     print "%%Page:", page, "\n"}

Explanation

#...
Comments
BEGIN { page = 0 }
BEGIN Rule: This is examined before the first line of the input file is read. Used e.g. to initialize variables
if ($0 ~/^%!PS-Adobe/) page++
Check for the string %!PS-Adobe at the start of a line and increment the page counter.
if (toupper($0) !~ /^%%PAGE:/) print $0
Show all Lines, which do not start with the string %%PAGE: .
if (toupper($0)~/^%%ENDCOMMENTS/) print "%%Page:", page, "\n"
Insert the actual page number after the line containing %%ENDCOMMENTS .


Martin P.J. Zinser