Insert Header Row using sed

converters, sed

Solution

do you really rename the old one to xxx.bak or can you just save a new copy?

either way, just use redirection.

`sed 1i"red|blue|green|orange|yellow" thefilename.dat > newfile.dat`

or if you want the .bak as well

sed 1i"red|blue|green|orange|yellow" thefilename.dat > newfile.dat \
&& mv thefilename.dat thefilename.dat.bak`

which would create your new file and then, only if the sed completed sucessfully, rename the orig file.

Problem

I need to run a bash script via cron to update a file. The file is a .DAT (similar to csv) and contains pipe separated values. I need to insert a header row at the top. Here's what I have so far: ``` #!/bin/bash # Grab the file, make a backup and insert the new line sed -i.bak 1i"red|blue|green|orange|yellow" thefilename.dat Exit ``` But how can I save the file as a different file name so that it always takes fileA, edits it and then saves it as fileB

Original source