tags:

views:

2069

answers:

7

I want to add a row of headers to an existing CSV file, editing in place. How can I do this?

echo 'one, two, three' > testfile.csv

and I want to end up with

column1, column2, column3
one,     two,     three

(nb changing the initial CSV output is out of my hands)

EDIT: I originally had this as 'using sed' but any standard command will do. The important thing is the file is edited in place, and the line is inserted at the beginning of the file. Thanks,

+1  A: 

This doesn't use sed, but using >> will append to a file. For example:

echo 'one, two, three' >> testfile.csv

Edit: To prepend to a file, try something like this:

echo "text"|cat - yourfile > /tmp/out && mv /tmp/out yourfile

I found this through a quick Google search.

Scottie T
Hi, this adds to the end of the file - I need it at the beginning - have clarified the question...
+1  A: 

sed is line based, so I'm not sure why you want to do this with sed. The paradigm is more processing one line at a time( you could also programatically find the # of fields in the CSV and generate your header line with awk) Why not just

echo "c1, c2, ... " >> file
cat testfile.csv >> file

?

Steve B.
+1  A: 

As far as I understand, you want to prepend column1, column2, column3 to your existing one, two, three.

I would use ed in place of sed, since sed write on the standard output and not in the file.

The command:

printf '0a\ncolumn1, column2, column3\n.\nw\n' | ed testfile.csv

should do the work.

perl -i is worth taking a look as well.

mouviciel
A: 

Note that for large text files, this is going to cause some real problems in terms of disk fragmentation and speed.

John
So from that point of view would it be better to create a new file, add headers, then the rest of the data from the old file? Speed is not a major concern but disk fragmentation might be.
+5  A: 

This adds custom text at the beginning of your file:

echo 'your_custom_escaped_content' > temp_file.csv
cat testfile.csv >> temp_file.csv
mv temp_file.csv testfile.csv
tunnuz
A: 

Use perl -i, with a command that replaces the beginning of line 1 with what you want to insert (the .bk will have the effect that your original file is backed up):

perl -i.bk -pe 's/^/column1, column2, column3\n/ if($.==1)' testfile.csv
+2  A: 

To answer your original question, here's how you do it with sed:

sed -i '1icolumn1, column2, column3' testfile.csv

The -i option also takes an optional argument to create a backup file, for example

sed -i~ '1icolumn1, column2, column3' testfile.csv

would keep the original file in "testfile.csv~".

Matthew Crumley
Which version of Unix? This does not work on Mac OS X Leopard, which is certified 'Single UNIX Specification V3'.
mouviciel
I use Linux. Apparently BSD sed (including OS X) requires an argument for -i (and should probably be separate like normal arguments). Try replacing the command with "sed -i '~' ...". If that works, I'll edit the answer.
Matthew Crumley
Thank you. I suspected this, but then I fail to make the i command work. I tried inline command (without or with -e) and script command (with -f) but I get: sed: rename(): Not a directory. You don't need to edit the answer as long as it is helpful for the asker.
mouviciel