Strange behaviour of awk

Kees Huizing wsinkees at eutrc3.UUCP
Fri Feb 24 21:17:27 AEST 1989


In the use of the UNIX filter "awk" I encountered the following problem.
I want to save a line of input except the first field into a variable
for later use.  So I empty the first field and then assign the modified
record to the variable.
But how curious!  When I try the test case
	{$1 = "" ; x = $0 ; print x}
on the input
	Tom Dick Harry
I just get :  
	Tom Dick Harry
Testing the value of $0 by printing it
	{$1 = "" ; print}
yields the expected output
	 Dick Harry
So my solution is:
	{$1 = "" ; print >"/dev/null" ; x = $0 ; print x}
And this works!, yielding
	 Dick Harry

Now I have two questions:
1. How can the value of $0 depend on whether it has been printed or not?
   Is this a bug, or do I overlook some mechanism of awk?
2. Is there a direct way to get $2, $3, .... until the end of the line (record)
   without the somewhat dirty change of $1?  This was my original problem.

P.S. We have Ultrix 2.2 (appr. Unix BSD); I don't know how "new" our awk is.

Kees Huizing              wsinkees at eutrc3.UUCP -or- wsdckees at heitue5.BITNET
Dept. of Math. and Comp. Sc. - Eindhoven Univ. of Techn. - Eindhoven
                    T H E     N E T H E R L A N D S



More information about the Comp.unix.questions mailing list