[comp.unix.wizards] Awk oddity

ado@elsie.UUCP (Arthur David Olson) (09/29/89)

A three-part exercise:

1.	Write and test an awk script to print all input lines that contain
	the character 'x'.
2.	Write and test an awk script to print all input lines that contain
	the character '='.
3.	Comment on the results.

Either "nawk" or "gawk" may be substituted for "awk" above.
-- 
	 To understand Sun's corporate culture look at the vi source code.
	Arthur David Olson   ado@alw.nih.gov   ADO is a trademark of Ampex.

ka@cs.washington.edu (Kenneth Almquist) (10/02/89)

> 2.	Write and test an awk script to print all input lines that contain
>	the character '='.

If you write

	awk '/=/'

awk will think that the sequence "/=" is an operator rather than the
start of a regular expression.  You can avoid this by writing

	awk '/\=/'

Or you can patch awk to handle this case.  (Look at the grammar rule
that matches a regular expression.  This handles a "/" operator.  Add
an alternative to handle the "/=" operator, which should be similar
except that it should do a "yyunput('=');" to cause the equals sign
treated as part of the regular expression.)
					Kenneth Almquist

dg@lakart.UUCP (David Goodenough) (10/02/89)

ado@elsie.UUCP (Arthur David Olson) asks:
> A three-part exercise:
> 
> 1.	Write and test an awk script to print all input lines that contain
> 	the character 'x'.
> 2.	Write and test an awk script to print all input lines that contain
> 	the character '='.
> 3.	Comment on the results.

1.
--- cut here --- cut here --- cut here --- cut here --- cut here ---
#! /bin/sh

awk '{
	for (i = 1; i <= length($0); i++)
	  if (substr($0, i, 1) == "x")
	    print $0
     }'
--- cut here --- cut here --- cut here --- cut here --- cut here ---

2.
--- cut here --- cut here --- cut here --- cut here --- cut here ---
#! /bin/sh

awk '{
	for (i = 1; i <= length($0); i++)
	  if (substr($0, i, 1) == "=")
	    print $0
     }'
--- cut here --- cut here --- cut here --- cut here --- cut here ---

3.
Huh?

Like the man said, "There's no problems, only solutions"
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com			  +---+

tad@prism.gatech.EDU (Tad K. Mannes) (10/05/89)

In article <708@lakart.UUCP> dg@lakart.UUCP (David Goodenough) writes:
> A three-part exercise:
> 
> 1.	Write and test an awk script to print all input lines that contain
> 	the character 'x'.
> 2.	Write and test an awk script to print all input lines that contain
> 	the character '='.
> 3.	Comment on the results.
>

	Whats wrong with the following scripts?

	awk '/\x/'

	awk '/\=/'

	That was'nt too difficult...
-- 
Tad K. Mannes
Georgia Institute of Technology, Atlanta Georgia, 30332
uucp: ...!{allegra,amd,hplabs,seismo,ut-ngp}!gatech!prism!tad
ARPA: tad@prism.gatech.edu