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

MOH*_*MED 19 linux bash shell awk

awk输出看起来像:

awk '{print $2}'
toto
titi
tata
Run Code Online (Sandbox Code Playgroud)

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

awk [option] '{print $2}'
toto titi tata
Run Code Online (Sandbox Code Playgroud)

有可能吗?

Man*_*y D 35

从联机帮助页:

ORS         The output record separator, by default a newline.
Run Code Online (Sandbox Code Playgroud)

因此,

awk 'BEGIN { ORS=" " }; { print $2 }' file
Run Code Online (Sandbox Code Playgroud)

  • 我会把它写成'awk'{print $ 2}'ORS =""file`,但这更像是你喜欢的选择. (8认同)

Jot*_*tne 17

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

awk '{printf "%s ",$2}' file
toto titi tata 
Run Code Online (Sandbox Code Playgroud)


بار*_*ابا 5

或者你可以使用paste

awk '{print $2}' FILE |paste -sd " "
Run Code Online (Sandbox Code Playgroud)