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?
To print a blank line, use print "" , where "" is the empty string. To print a fixed piece of text, use a string constant, such as "Don't Panic" , as one item. If you forget to use the double-quote characters, your text is taken as an awk expression, and you will probably get an error.
Default behavior of Awk: By default Awk prints every line of data from the specified file. In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure.
awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.
If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.
From the manpage:
ORS The output record separator, by default a newline.
Therefore,
awk 'BEGIN { ORS=" " }; { print $2 }' file
You can always use printf
to control the output of awk
awk '{printf "%s ",$2}' file
toto titi tata
OR you can use paste
awk '{print $2}' FILE |paste -sd " "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With