Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Column Headers in CSV with AWK in Terminal

Hello I'm trying to add column headers in my CSV file with awk in terminal.

Currently displays:

Michael, [email protected], Seattle, Washington, detail1, detail2, detail3

Desired format:

Name, Email, City, State, more1, more2, more3etc
Michael, [email protected], Seattle, Washington, detail1, detail2, detail3,

Have tried something like this, but I end up getting "Illegal Statement at Source line 2":

awk 'BEGIN {FS=",";OFS=","; print "Rating,Restaurant,Address,Landline,Mobile,Cuisine,Website,LatDD,LonDD"}
NR >1 {print ",",,,",",",", }' Restaurants.txt > Restaurants_newer.txt
like image 608
Michael Avatar asked Aug 27 '26 03:08

Michael


1 Answers

this will do

sed '1iName, Email, City, State, more1, more2, more3etc' file > newfile

can be done in place too

sed -i '1iName, Email, City, State, more1, more2, more3etc' file

or

{ echo 'Name, Email, City, State, more1, more2, more3etc'; cat file; } > newfile
like image 195
karakfa Avatar answered Aug 29 '26 16:08

karakfa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!