Karen
Karen

Reputation: 173

Insert Header Line into CSV File

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.

Upvotes: 0

Views: 4429

Answers (2)

Abu Shoeb
Abu Shoeb

Reputation: 5152

I would use the Sed command

sed -i '1s/^/SYSNAME,OBJSTRUC,AddChange,EN\n/' filename.csv

Upvotes: 0

geisterfurz007
geisterfurz007

Reputation: 5874

@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).

Upvotes: 1

Related Questions