send the unix output to a csv file

后端 未结 5 836
野的像风
野的像风 2021-02-06 13:11

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

5条回答
  •  -上瘾入骨i
    2021-02-06 13:23

    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
    

提交回复
热议问题