user4812
user4812

Reputation: 6722

How can I add a line to a file in a shell script?

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.

Upvotes: 55

Views: 187317

Answers (8)

Hutch
Hutch

Reputation: 173

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"

Upvotes: 0

tflutre
tflutre

Reputation: 3546

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

Upvotes: 1

Matthew Crumley
Matthew Crumley

Reputation: 102735

To answer your original question, here's how you do it 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~".

Upvotes: 80

Markus
Markus

Reputation:

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  

Upvotes: 1

tunnuz
tunnuz

Reputation: 23988

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

Upvotes: 40

Scottie T
Scottie T

Reputation: 12195

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.

Upvotes: 25

mouviciel
mouviciel

Reputation: 67839

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.

Upvotes: 9

Steve B.
Steve B.

Reputation: 57324

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

?

Upvotes: 3

Related Questions