Adding Column Headers in CSV with AWK in Terminal

12,831

Solution 1

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

Solution 2

awk '
BEGIN { OFS=", "; print "Name", "Email", "City", "State", "more1", "more2", "more3etc" }
{ print $0, "" }
' file
Share:
12,831
Michael
Author by

Michael

Updated on June 05, 2022

Comments

  • Michael
    Michael almost 2 years

    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