[comp.unix.questions] Supressing new-lines in awk output

root@conexch.UUCP (Larry Dighera) (02/03/89)

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,
such that the output would look like:

	field1.1 field2.1 field3.1


-- 
USPS: The Consultants' Exchange, PO Box 12100, Santa Ana, CA  92712
TELE: (714) 842-6348: BBS (N81); (714) 842-5851: Xenix guest account (E71)
UUCP: conexch Any ACU 2400 17148425851 ogin:-""-ogin:-""-ogin: nuucp
UUCP: ...!uunet!turnkey!conexch!root || ...!trwrb!ucla-an!conexch!root

jim@tiamat.fsc.com (Jim O'Connor) (02/05/89)

In article <21638@conexch.UUCP>, root@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

itkin@mrspoc.UUCP (Steven M. List) (02/05/89)

In article <21638@conexch.UUCP> root@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

pss@unh.UUCP (Paul S. Sawyer) (02/07/89)

In article <21638@conexch.UUCP>, root@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
> 
Try:

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

(You would need

	awk '{ printf ("%s\n", $1) }' filename

to equal the unformatted awk "print" statement)  You can also do tricks with
setting a variable to be a space for, say 5 input lines and a newline on the
sixth for six column output.  Awk is really quite versatile (even oawk), if
you are stubborn enough!

rupley@arizona.edu (John Rupley) (02/07/89)

In article <21638@conexch.UUCP>, root@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

If you have the new awk == nawk:
        nawk 'BEGIN{ORS=" "}; {print $1}' filename

John Rupley      rupley!local@megaron.arizona.edu

matthew@sunpix.UUCP ( Sun NCAA) (02/08/89)

In article <21638@conexch.UUCP>, root@conexch.UUCP (Larry Dighera) writes:
> Is there a way to supress the newline at the end of awk's print output?

Try using awk's printf command.  If you don't include a '\n' in the format
specifier, non will be printed.

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

> Is there a way to cause each "$1" to be printed with a space delimiter 

Notice space in above example. A '\t' could also be used to insert a tab.


-- 
Matthew Lee Stier     (919) 469-8300|
Sun Microsystems ---  RTP, NC  27560|          "Wisconsin   Escapee"
uucp: {sun, rti}!sunpix!matthew     |

uucibg@sw1e.UUCP (3929]) (02/09/89)

In article <388@greens.UUCP> matthew@sunpix.UUCP ( Sun NCAA) writes:
>In article <21638@conexch.UUCP>, root@conexch.UUCP (Larry Dighera) writes:
>> Is there a way to supress the newline at the end of awk's print output?
>
>Try using awk's printf command.  If you don't include a '\n' in the format
>specifier, non will be printed.
>
>awk '{printf "%s ", $1} filename
>

Also, in the body for a BEGIN pattern, you can set the output record
separator to nothing via:
	BEGIN { ... ORS = "" ; ... }

I know this works for the new awk, and I'm not sure but I believe that it's
one of the undocumented features of the standard awk (Note however, that
the currently standard 'awk' will become 'oawk' in SysV.4 and the current
'nawk' will become the standard 'awk').

Note also that you can change ORS anywhere in the awk program, not just in
BEGIN clauses.  This is nice if you want to save the current separator and
temporarly use something different, restoring the original value after you're
through.

>> Is there a way to cause each "$1" to be printed with a space delimiter 
>
>Notice space in above example. A '\t' could also be used to insert a tab.
>

Similar to ORS above, OFS is the output field separator and can be set to
whatever you like (e.g. '... OFS = "<silly field separator string>" ...' ).

>Matthew Lee Stier     (919) 469-8300|
>Sun Microsystems ---  RTP, NC  27560|          "Wisconsin   Escapee"
>uucp: {sun, rti}!sunpix!matthew     |

Brian R. Gilstrap                          Southwestern Bell Telephone
One Bell Center Rm 17-G-4                  ...!ames!killer!texbell!sw1e!uucibg
St. Louis, MO 63101                        ...!bellcore!texbell!sw1e!uucibg
(314) 235-3929
#include <std_disclaimers.h>

steinar@fdmetd.uucp (Steinar Overbeck Cook) (02/10/89)

In article <21638@conexch.UUCP>, root@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
> 

Use this instead:

	awk '{ printf("%s", $1) }'


-- 
Steinar Overbeck Cook, Fellesdata a.s, P.O. Box 248, 0212 OSLO 2, NORWAY
Phone : +47 2 52 80 80                            Fax   : +47 2 52 85 10
E-mail : ...!mcvax!ndosl!fdmetd!steinar  or       steinar@fdmetd.uucp
<The opinions expressed, if any, do not represent Fellesdata a.s>