bashcsvawksed

Adding headers to sometimes empty CSV file via bash script


I am trying to add a few headers to occasionally empty CSV file (so sometimes the file will be empty and sometimes it will have data) via bash. So far I have tried using sed and awk.

Sed:

sed -i '1liAge,Sex,Location' $nameOfFile

Awk:

awk 'BEGIN{print "Age,Sex,Location"}1' $nameOfFile

Both of these do not appear to work since no headers are added. When I echo $nameOfFile, csvInfo.csv is printed so I don't believe there's anything wrong with $nameOfFile. Not sure where i'm going wrong here.


Solution

  • Your sed command requires there to already be at least one line in the file, so I don't think you can make that work reliably.

    The Awk command you have should work as such, but it writes its results to standard output. If you have GNU Awk, you can use -i inplace to get behavior similar to sed -i; but the portable solution is to write the results to a temporary file, then move back on top of the old file.

    awk 'BEGIN{print "Age,Sex,Location"}1' "$nameOfFile" >tmp
    mv tmp "$nameOfFile"
    

    Notice also When to wrap quotes around a shell variable.

    If you want to avoid adding a header when it's already there, you can keep the BEGIN block, but suppress printing of the old header, if present:

    awk 'BEGIN{print "Age,Sex,Location"}
        NR>1 || $0 !~ /^Age,Sex,Location$/' "$nameOfFile" >tmp
    mv tmp "$nameOfFile"
    

    Proper hygiene around temporary files is somewhat more involved for a production script, but if this is just for your personal use, let's leave it at this. For more information, perhaps review Removing created temp files in unexpected bash exit