Supressing new-lines in awk output

Steven M. List itkin at mrspoc.UUCP
Sun Feb 5 09:05:43 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
>...
> Is there a way to cause each "$1" to be printed...
> such that the output would look like:
> 
> 	field1.1 field2.1 field3.1
> 
Yup - use the "printf" function instead of "print".  "printf" takes 
arguments like the C language function to permit more advanced formatting
of your output.

To solve this problem, use

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

This will print the first field of each record followed by a space.  To
include the newline at the end of the entire list, use the following variant:

	awk '{printf "%s ",$1}END{printf "\n"}' filename
-- 
:  Steven List @ Transact Software, Inc.
:  {apple,coherent,limbo,mips,pyramid,ubvax}!mrspoc!itkin
:  Voice: (415) 961-6112



More information about the Comp.unix.questions mailing list