3

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
4
  • 2
    Does this problem reduce to "Insert a line at the beginning of my file"? Commented May 13, 2016 at 17:39
  • 1
    And why is the first line in the desired format different from what you try to insert in your awk command? Commented May 13, 2016 at 17:40
  • 1
    The second print has syntax errors: too many ,. Commented May 13, 2016 at 17:51
  • for BEGIN use print "Rating", "Restaurant" and so on, with each header as one string and the different headers separated by a comma. Replace the second NR >1 ... ,} with {print } or just 1. Commented May 13, 2016 at 17:54

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but... I'm gettings this on terminal: > command i expects \ followed by text
Not sure, and OSX.
3
awk '
BEGIN { OFS=", "; print "Name", "Email", "City", "State", "more1", "more2", "more3etc" }
{ print $0, "" }
' file

2 Comments

Hmmm, this isn't working. My CSV file still shows up blank after I tried this. Thanks though.
The script cannot do what you claim it is doing. When you say "My CSV file..." idk if you mean your input file or your output file but that script cannot empty your input file since it doesn't write to it and it cannot produce an empty output file since the BEGIN section will print a line no matter what. You're doing something wrong so edit your question to copy/paste the full command line you are running and the output it produces so we can see what you're doing to help you debug it.
1

Using AWK :

awk 'BEGIN{print "Name, Email, City, State, more1, more2, more3 .."}1' Restaurants.txt

OR

Try this :

echo "Name, Email, City, State, more1, more2, more3 .." >> Restaurants_newer.txt
cat Restaurants.txt >> Restaurants_newer.txt

That is redirect the header line & the file output into a file.

OR

{ printf 'Name, Email, City, State, more1, more2, more3 ..'; cat Restaurants.txt; } > Restaurants_newer.txt

1 Comment

Thanks for your help, but... I'm gettings this on terminal: > command i expects \ followed by text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.