🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.misc.discuss
Article #63359

GAWK Question

#63359
From: JoAnne Bassett
Date: Wed, 08 Feb 1995 07:59
40 lines
1958 bytes
Is there an elegant way to have a 'gawk' script act like a Unix filter yet
have a 'help' paragraph automatically written when the script is improperly
used?  Specifically, how do I get it to behave as a filter at the end of a
pipe?  Here's some code that I've been dinging with.  The script name
is gawker.  Thanks for any help!
------------------------------------------------------------------------------
#!/usr/bin/gawk -f
BEGIN	{
	if (ARGC == 1) {
		print "Usage: gawker file.in > file.out\n"
		print "       Filter data\n"
		print "Input:   file.in   <STDIN>"
		print "Output:  file.out  <STDOUT>"
		exit
	}
	print "begin action"
}
	{
	print		# echo standard input to standard output
}
END	{
	if (ARGC == 1) exit
	print "end action"

--------------------------------------------------------------------------
$ gawker
Usage: gawker file.in > file.out

       Filter data

Input:   file.in   <STDIN>
Output:  file.out  <STDOUT>
$ gawker file.in > file.out		# behaves OK
$ cat file.in | gawker > file.out	# bombs

-- 
JoAnne Bassett                          Aromatherapy grade essential oils,
Certified Aromatherapist       >><<     blends, diffusers, floral waters,
joanne@eskimo.com              >><<     natural bodycare products, kits,
(206) 451-0845                 >><<     blending oils, books, etc.
Technical Details
Message-ID: <D3o8uM.L68@eskimo.com>
Path: arc1-ca.pugleaf.net!swrinde!cs.utexas.edu!convex!news.duke.edu!news.mathworks.com!mvb.saic.com!eskimo!joanne

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1025b4,5b6a69d7b3a4f933,start
X-Google-Attributes: gid1025b4,public
X-Google-ArrivalTime: 1995-02-08 00:29:58 PST
Newsgroups: gnu.misc.discuss
Path: arc1-ca.pugleaf.net!swrinde!cs.utexas.edu!convex!news.duke.edu!news.mathworks.com!mvb.saic.com!eskimo!joanne
From: joanne@eskimo.com (JoAnne Bassett)
Subject: GAWK Question
X-Nntp-Posting-Host: eskimo.com
Message-ID: <D3o8uM.L68@eskimo.com>
Sender: usenet@eskimo.com (News User Id)
Organization: Eskimo North (206) For-Ever
X-Newsreader: TIN [version 1.2 PL2]
Date: Wed, 8 Feb 1995 07:59:09 GMT
Lines: 40