Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: Ps,Apr,2009,Admin

Postscript Tricks

These pages focus on postscript tricks, written in Awk.


categories: Ps,Apr,2009,ArnoldR

pschoose.awk

Contents

Synopsis

Download

Description

Details

Code

Author

Synopsis

gawk -f pschoose

Download

Download from LAWKER

Description

Pulls out a range of pages from postscript and just print those.

Details

Pagerange : list of pages from command line.

Pages : array with broken out list.

At end: "(n in Pages)" is true if page n should be printed

Code

Set up the list of paes to print.
function set_pagerange(        n, m, i, j, f, g)
{
	delete Pages

	n = split(Pagerange, f, ",")
	for (i = 1; i <= n; i++) {
		if (index(f[i], "-") != 0) { # a range
			m = split(f[i], g, "-")
			if (m != 2 || g[1] >= g[2]) {
				printf("bad list of pages: %s\n",
					f[i]) > "/dev/stderr"
				exit 1
			}
			for (j = g[1]; j <= g[2]; j++)
				Pages[j] = 1
		} else
			Pages[f[i]] = 1
	}
}

BEGIN {
	# constants
	TRUE = 1
	FALSE = 0

	if (ARGC != 3) {
		print "usage: pschoose range-spec file\n" > "/dev/stderr"
		exit 1
	}
	Pagerange = ARGV[1]
	delete ARGV[1]
	set_pagerange()
}

NR == 1, /^%%Page:/ {
	if (! /^%%Page/) {
		Prolog[++nprolog] = $0
		next
	}
}

/^%%Trailer/ || In_trailer {
	In_trailer = TRUE
	Epilog[++nepilog] = $0
	next
}

/^%%Page: /	{
	++Npage
	line = 0
}

 for all non-special lines
{
	# only save it if we will want to print it
	if (Npage in Pages)
		Page[Npage, ++line] = $0
}

END {
	# print the prologue
	for (i = 1; i in Prolog; i++)
		print Prolog[i]

	# print the actual body
	for (i = 1; i <= Npage; i++) {
		if (i in Pages) {
			for (j = 1; (i, j) in Page; j++) {
				print Page[i, j]
			}
		}
	}

	# print the epilog
	for (i = 1; i in Epilog; i++)
		print Epilog[i]
}

Author

Arnold Robbins


categories: Ps,Apr,2009,ArnoldR

psrev.awk

Contents

Synopsis

Download

Description

Code

Author

Synopsis

gawk -f psrev.awk

Download

Download from LAWKER

Description

Reverse the pages in a postscript file.

Code

BEGIN {
	# constants
	TRUE = 1
	FALSE = 0

	# Initialize global booleans
	Twoup = FALSE

	# process command line flags
	for (i = 1; i in ARGV && ARGV[i] ~ /^-/; i++) {
		if (ARGV[i] == "-2")
			Twoup = TRUE
		else
			printf("psrev: unrecognized option %s\n",
				ARGV[i]) > "/dev/stderr"
		delete ARGV[i]
	}
}

NR == 1, /^%%Page:/ {
	if (! /^%%Page/) {
		Prolog[++nprolog] = $0
		next
	}
}

/^%%Trailer/ || In_trailer {
	In_trailer = TRUE
	Epilog[++nepilog] = $0
	next
}

/^%%Page: /	{
	++Npage
	line = 0
}

 for all non-special lines
{
	Page[Npage, ++line] = $0
}

END {
	# print the prologue
	for (i = 1; i in Prolog; i++)
		print Prolog[i]

	# print the actual body
	if (Twoup) {
		hasodd = (Npage %2 == 1)
		if (hasodd) {
			# print last page
			for (j = 1; (Npage, j) in Page; j++)
				print Page[Npage, j]
			# make a fake last page for psnup
			printf "%%%%Page: %d %d\n", Npage+1, Npage+1
			printf "showpage\n"
			print "%%BeginPageSetup"
			print "BP"
			print "%%EndPageSetup"
			print "EP"
		}
		lastpage = (hasodd ? Npage - 1 : Npage)
		for (i = lastpage; i > 0; i -= 2) {
			for (k = i - 1; k <= i; k++)
				for (j = 1; (k, j) in Page; j++)
					print Page[k, j]
		}
	} else {
		# regular 1 up printing
		for (i = Npage; i > 0; i--)
			for (j = 1; (i, j) in Page; j++)
				print Page[i, j]
	}

	# print the epilog
	for (i = 1; i in Epilog; i++)
		print Epilog[i]
}

Author

Arnold Robbins

blog comments powered by Disqus