I want to put the output data from unix command to a csv file. Suppose the output which I am getting is :
A
B
C
I want to put this data in .csv file as
A B C
in three different columns but same row.
Try this :
printf '%s\n' A B C | paste -sd ' ' >> file.csv
or more classical for a CSV (delimiter with a ,
:
printf '%s\n' A B C | paste -sd ',' >> file.csv
printf '%s\n' A B C
is just an example to have the same sample input as you. My solution works with spaces in a same line too.
EDIT from your comments, you seems to need to treat with a for
loop, so :
for i in {0..5}; do printf '%s\n' {A..C} | paste -sd " " >> file.csv; done
or in pseudo code :
for ...:
unix_command | paste -sd " " >> file.csv
endfor
unix_command | tr "\n" " " > file.csv
or
unix_command | awk 'ORS=FS' > file.csv
Disadvantage in both cases: one trailing space
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