chrstahl89
chrstahl89

Reputation: 590

Create a CSV from Text File useing Linux Console

I have a text file that looks like this:

    line1
    line2
    line3
    line4
    line5
    "" "" keep going for a long time

I'm trying to come up with a script that would give me:

    line1,line2,line3,line4,line5
    line6,line7,line8,line9,line10

So comma separate them all and add a newline every 5. Any ideas?

Upvotes: 1

Views: 4840

Answers (5)

Paul Rubel
Paul Rubel

Reputation: 27232

A bit rough but workable w/out resorting to the, perhaps more pleasing, perl solution. The -n 5 arg to xargs makes it just send 5 arguments to the shell script, which we print.

$ cat echo.sh 
echo $1,$2,$3,$4,$5
$ $ cat file.txt 
a
b
c
d
e
1
2
3
4
5
$ cat file  | xargs -n 5 ./echo.sh
a,b,c,d,e
1,2,3,4,5
$

Upvotes: 1

Damon
Damon

Reputation: 687

cat foo.txt | xargs -L 5 | tr ' ' ','

The plus side here is that you can also modify the '5' arguments to an arbitrary value and the script works as expected.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247012

paste -d, - - - - - < filename

man page

Upvotes: 4

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

Python is installed by default on every linux distro these days.

I'd suggest the following python script:

#!/usr/bin/env python
import argparse, csv
if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='convert text to csv', version='%(prog)s 1.0')
    parser.add_argument('infile', nargs='+', type=str, help='list of input files')
    parser.add_argument('--out', type=str, default='temp.csv', help='name of output file')
    args = parser.parse_args()

    writer = csv.DictWriter(open(args.out, "wb"), ["field 1","field 2","field 3","field 4","field 5"], dialect='excel')
    # write the header at the top of the file
    writer.writeheader()
    row = []

    for fname in args.infile:
        with open(fname) as df:
            for line in df.readlines():
                row.append(line.strip('\n'))
                if len(row) = 5:
                    writer.writerow(row)
                    row = []
    del writer

You should be able to copy the code into a file and run it right off the command line. For instance: text2csv.py yourinput.txt if, of course, you called the file text2csv.py.

Upvotes: 0

jejese
jejese

Reputation: 111

If you have perl, try this:

perl -ane '++$i; chomp; $line.=$_; if($i==5) {print "$line\n"; $line=""; $i=0;} else {$line.=","} ' <infile >outfile

Upvotes: 0

Related Questions