7

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.

2
  • 6
    Okay, great. You've described the problem you're trying to solve. Now, tell us what steps you've taken in your attempts to solve it. Commented Dec 9, 2014 at 20:27
  • I tried using operator >> to csv file Commented Dec 9, 2014 at 20:29

5 Answers 5

10

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
Sign up to request clarification or add additional context in comments.

8 Comments

Actually these outputs SAY A B C will be the words and will be stored in a text file
Isn't what you expected ? If not, be more concise please.
Sorry,Trying to be more concise.See I will run a for loop and from each loop i will get 3 results,I am trying to put this 3 results in csv file
Hummmm, this is exactly the purpose of my snippet above Django.
for every output the data need to be in column and for every for loop the result should be in next row
|
7
unix_command | tr "\n" " " > file.csv

or

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

Disadvantage in both cases: one trailing space

Comments

3

For my understanding, @Django needs three line into one line.

paste -d ' ' - - - < infile

If you need output as csv format (split by ,), you can use this

paste -d ',' - - - < infile

Here is the test result

$ cat infile
Manoj Mishra
Japan
Environment.
Michael Jackson
America
Environment.

$ paste -d ',' - - - < infile

Manoj Mishra,Japan,Environment.
Michael Jackson,America,Environment.

4 Comments

Sorry but your snippet is not working in my case.There are space and i am not getting what is infile here
mate, you need provide sample input first.
ok Consider the sample input which are in text file.First Line-Manoj Mishra.2nd line Japan .3rd line Environment.
there will be many lines but i want to get three lines/output one by one and have to print in csv in first 3 columns,next 3 lines/output will be in 1st 3 columns of next row of csv file
1

A more general answer

If the output of your command is multi-line and you want to put the quoted output in csv format, n items per line, the following script could be handy.


The groupby program reads from stdin and

  1. quotes each input line
  2. groups n quoted input lines in a csv record, using a comma as a separator

optionally, using the -s optional argument, the program discards the last line of its output if said last line doesn't contain exactly n items.

The -h option, as usual, echoes an usage line and exits.

Specifying another option the program prints the usage line and exits in error.

The code

% cat groupby
#!/bin/sh
usage () { echo Usage: $0 [-s] n --- -s is for \"strict\", outputs only records of n items. ; exit $1 ; }
s=0
while getopts :sh o ; do
    case "${o}" in
        s) s=1 ; shift ;;
        h) usage 0     ;;
        *) usage 1     ;;
    esac
done
awk -v n=$1 -v s=$s -v q='"' '
NR==1   {buf = q $0 q ; next}
NR%n==1 {print buf; buf = q $0 q ; next}
        {buf = buf "," q $0 q}
END     {if(!s||NR%n==0)print buf}'
%

An example of usage

% chmod +x groupby
% echo -e "1\n2\n3\n4\n5" | ./groupby 3
"1","2","3"
"4","5"
% echo -e "1\n2\n3\n4\n5\n6" | ./groupby 3
"1","2","3"
"4","5","6"
echo -e "1\n2\n3\n4\n5\n6\n7" | ./groupby 3
"1","2","3"
"4","5","6"
"7"
% echo -e "1\n2\n3\n4\n5\n6\n7\n8" | ./groupby -s 4
"1","2","3","4"
"5","6","7","8"
% echo -e "1\n2\n3\n4\n5\n6\n7" | ./groupby -s 4
"1","2","3","4"
%

A different angle

I changed the defaults to suit best the OP requirements, and introduced other options, see the usage string for details

#!/bin/sh
usage () { echo 'Usage: '$0' [-s] [-q quote_char] [-c separator_char] n
  Reads lines from stdin and prints them grouped by n and separated by spaces.
  Optional arguments:
    -s is for "strict", outputs only records of n items;
    -q quote_char, forces quoting of each input line;
    -c separator_char, changes the field separator,
       interesting alternatives are tab, comma, semicolon etc;
    -h prints this help and exits.' ; exit $1 ; }

# Default options
s=0 ; q='' ; c=' '

# Treatment of optional arguments
while getopts :shc:q: o ; do
    case "${o}" in
        s) s=1 ;         ;;
        c) c="${OPTARG}" ;;
        q) q="${OPTARG}" ;;
        h) usage 0       ;;
        *) usage 1       ;;
    esac
done
shift $(($OPTIND-1))

# awk code
awk -v n=$1 -v s=$s -v q="$q"  -v c="$c" '
    NR==1   {buf = q $0 q ; next}
    NR%n==1 {print buf; buf = q $0 q ; next}
            {buf = buf c q $0 q}
    END     {if(!s||NR%n==0)print buf}'

Comments

-3

just use xargs.


eg:

less filename| xargs >> filename.csv

1 Comment

... | xargs -n3 > ... would create rows with three elements each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.