Strange behaviour of awk

Joe Smith jes at mbio.med.upenn.edu
Mon Feb 27 16:09:35 AEST 1989


>   Is this a bug, or do I overlook some mechanism of awk?

The AWK book says that "when one of $1, $2, etc., is changed, $0 is
reconstructed using OFS to separate fields".  So it's definitely a
bug.  However, unless you are using System V, you probably have old
awk which may have bugs that have been fixed in new awk.  Our (SunOS
4.0) old awk suffers the same bug.

>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.

You could just concatenate the fields with a simple loop:

	x = ""
	for (f = 2; f < NF; ++f)	# skip $1
		x = x " " $f		# fields separated by space
	print x

That won't preserve the spacing from the input line, but I don't think *any*
awk will do that.  If the spacing is important, maybe you could use sed:

	sed 's/^ *[^ ]* *//' file


--
jes at mbio.med.upenn.edu

University of Pennsylvania
Dept. of Biochemistry and Biophysics
233 Anatomy-Chemistry
Philadelphia, PA 19104-6059
(215) 898-8348



More information about the Comp.unix.questions mailing list