0

I have a CSV file and I want to insert a header line to the CSV file.

The header will be:

SYSNAME,OBJSTRUC,AddChange,EN

When I use:

echo hello new line > filename.csv

it adds the new line but deletes everything from the CSV file.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Karen
  • 123
  • 2
  • 4
  • 14
  • Related: [Adding text to the begining of many existing .txt files using a batch file](http://stackoverflow.com/q/41290680) and [How to add text from a file as a header into multiple files which are in separate folder?](http://stackoverflow.com/q/40541597) – aschipfl Jan 23 '17 at 14:46

2 Answers2

1
@echo off
echo SYSNAME,OBJSTRUC,AddChange,EN > csv.tmp
type your.csv >> csv.tmp
del your.csv
ren csv.tmp your.csv

Using the redirection parameters you can first echo the header in a temporary file and then type the contents of your csv-file right after.
Then delete the original .csv and rename the temporary one to the original one.

Note the difference between > (overwrite the file with redirected text) and >> (append to the bottom of redirection context).

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • thank you, this makes sense however, is there a meaning for >>> ? i was also wondering why it adds the line to the bottom and not the top? – Karen Jan 23 '17 at 14:08
  • 1) Not that I would know. 2) There has to be a standard. And it is easier to add a line to the bottom. If adding to the top you would have to first shift all the content over and then replace the free place with the things you need. At least that is how the program would do I guess. – geisterfurz007 Jan 23 '17 at 14:23
  • thanks, this worked well anyway :) do you know how i would go about taking text from a filename and adding that to a new line? – Karen Jan 23 '17 at 14:35
0

I would use the Sed command

sed -i '1s/^/SYSNAME,OBJSTRUC,AddChange,EN\n/' filename.csv
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45