是否可以在同一行中打印awk输出?



awk输出如下:

awk '{print $2}'
toto
titi
tata

我想在同一行中显示awk的输出,用空格作为分隔符,而不是新行

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

这可能吗?

从手册:

ORS         The output record separator, by default a newline.
因此,

awk 'BEGIN { ORS=" " }; { print $2 }' file

您始终可以使用printf来控制awk的输出

awk '{printf "%s ",$2}' file
toto titi tata 

或者您可以使用paste

awk '{print $2}' FILE |paste -sd " "

我的选项与接受的答案相同,但我相信我的命令有点容易记住。

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

最新更新