[net.bugs.v7] V7 awk bug

henry@utzoo.UUCP (Henry Spencer) (03/20/84)

I just ran into an interesting bit of misbehavior by awk.  I'm processing
a file which looks roughly like this:

	private	$1.00	3600	seconds
	public	$1.00	3600	seconds
	dialup	$2.00	3600	seconds
	disk	$1.00	1000	block-days

with the following awk program (stripped of some irrelevancies):

	BEGIN { FS = "\t" ; OFS = "\t" }
	/./{
		n = split(substr($2, 2), dolcent, ".")
		print n, $1, dolcent[1] dolcent[2], $3
	}

We get rid of $ by taking the substr from the second character on, and
then split it and output the result.  Guess what?  For the very first
line in the file, the split yields n == 0 (!).  The line itself is not
to blame:  I tried reordering the lines, and the problem still appears
on the first and only the first.  Inserting a trash line (which gets
kicked out by some earlier checking) in front doesn't affect the result.
Changing the "split(..." line to:

		it = substr($2, 2)
		n = split(it, dolcent, ".")

make the problem go away!  For some reason split doesn't like having
a substr as its first argument... once!

I have not investigated the problem in depth, since I lack both the time
and the courage to go wading into awk.  Any brave souls out there?
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

liberte@uiucdcs.UUCP (liberte ) (03/29/84)

#R:utzoo:-366400:uiucdcs:8300001:000:751
uiucdcs!liberte    Mar 28 15:58:00 1984


We worked on figureing out what the bug might be on our Berkeley 4.1 and 4.2
and learned the following:  

on 4.1

- If you repeat the split(... line , it doesnt work in a different way and
comes up with some garbage.  If you repeat it again (three times) then it
works correctly.

- If you use a literal string instead of $2, it may work if another call
to split interferes in some way - an order dependency.  If you use a 
regular variable with a string value instead, it doesnt work in another way,
sometimes.

on 4.2

- Field variables seemed to work correctly, but the regular variable had
yet another different problem.  This version of awk includes a bug fix
from net.bugs.4bsd by sun!shannon, Dec 10, 1983

Daniel LaLiberte
(uiucdcs!liberte)