56

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

Changing the initial CSV output is out of my hands.

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.

0

9 Answers 9

83

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

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

The 1i command tells sed to go to line 1 and insert the text there.

The -i option causes the file to be edited "in place" and can also take 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~".

Sign up to request clarification or add additional context in comments.

5 Comments

Which version of Unix? This does not work on Mac OS X Leopard, which is certified 'Single UNIX Specification V3'.
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.
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.
Great, but i think you should note the "1i" part, it's not obvious unless you know sed.
I think this should be the right answer to the question, cause you did not copy file or make a temporary file , this works will if you have a big file
41

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

1 Comment

this is more generic for all shell languages
27

Without sed, you can use >> to append to a file. For example:

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

To prepend to a file, try something like:

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

2 Comments

Hi, this adds to the end of the file - I need it at the beginning - have clarified the question...
This solution is also useful when you have limited space and a big file, you can use it like this: echo "text"|cat - sourcefile | gzip >tmpfile.gz; rm -f sourcefile; ungzip tmpfile.gz
9

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 writes to stdout instead of the file:

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

perl -i is worth taking a look at as well.

2 Comments

For completeness, to append you would just have printf 'hello world' >> testfile.txt
Consider printf '%s\n' 0a "column1, column2, column3" . w as possibly clearer than lots of embedded \n.
3

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

?

Comments

1

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  

Comments

1

Add a given line at the beginning of a file in two commands:

cat <(echo "blablabla") input_file.txt > tmp_file.txt
mv tmp_file.txt input_file.txt

Comments

0

To make an in-place change, the correct tool is ed:

ed testfile.csv <<END
0i
column1, column2, column3
.
wq
END

Unlike sed -i or any shell function using mv, this overwrites the file with the new contents, rather than replacing with a new file.

Comments

-1

how to add line inside a file

sed -i -e "48r../../../../folder1/lines_to_add.txt" a.txt 

a.txt - the file you want to change 48r is line number of a.txt some lines are inside lines_to_add.txt file

../../../../scripts3_2d/lines_to_add.txt 

-i update a.txt, try without -i before run the code, be careful with new lines,

"keep a newline at the end of lines_to_add.txt"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.