Supressing new-lines in awk output

Jim O'Connor jim at tiamat.fsc.com
Sun Feb 5 05:10:06 AEST 1989


In article <21638 at conexch.UUCP>, root at conexch.UUCP (Larry Dighera) writes:
> Is there a way to supress the newline at the end of awk's print output?
> Given:
> 	awk '{print $1}' filename
> Where filename contains:
> 	field1.1 field1.2
> 	field2.1 field2.2
> 	field3.1 field3.2
> awk will print the first field of each record on a seperate line:
> 	field1.1
> 	field2.1
> 	field3.1
> Is there a way to cause each "$1" to be printed with a space delimiter instead,

awk '	{printf "%s ", $1}
END	{ print }' filename

should do the trick.  The END action is needed or you won't get a newline at
all.

Just like printf() in the C library, the awk printf needs "\n" at the end
of the format string if you want a newline.

--jim



More information about the Comp.unix.questions mailing list