Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send the unix output to a csv file

Tags:

bash

unix

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.

like image 882
Django Avatar asked Dec 09 '14 20:12

Django


2 Answers

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
like image 119
Gilles Quenot Avatar answered Sep 21 '22 12:09

Gilles Quenot


unix_command | tr "\n" " " > file.csv

or

unix_command | awk 'ORS=FS' > file.csv

Disadvantage in both cases: one trailing space

like image 40
Cyrus Avatar answered Sep 21 '22 12:09

Cyrus