35

The awk output looks like:

awk '{print $2}'
toto
titi
tata

I want to dispay the output of awk in the same line with space as separator instead of new line

awk [option] '{print $2}'
toto titi tata

Is it possible to do that?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

4 Answers4

58

From the manpage:

ORS         The output record separator, by default a newline.

Therefore,

awk 'BEGIN { ORS=" " }; { print $2 }' file
Manny D
  • 20,310
  • 2
  • 29
  • 31
  • 14
    I would have written it like this `awk '{print $2}' ORS=" " file`, but this is more of choice of what you like. – Jotne Jun 20 '14 at 17:27
32

You can always use printf to control the output of awk

awk '{printf "%s ",$2}' file
toto titi tata 
Jotne
  • 40,548
  • 12
  • 51
  • 55
5

OR you can use paste

awk '{print $2}' FILE |paste -sd " "
Baba
  • 852
  • 1
  • 17
  • 31
3

My option is the same as accepted answer but I believe my command is a bit easy to remember.

awk '{print $2 }' ORS=' '

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69