emily wilson
emily wilson

Reputation: 35

using awk in command line to add column to end of csv

I have downloaded this CSV file: https://www.nhsbsa.nhs.uk/sites/default/files/2020-05/Dispensing%20Data%20Jan%2020%20-%20CSV.csv

and am trying to add a column on to the end with a value of "null" for every row.

I have tried using this awk command: awk 'BEGIN{FS=OFS=","}{print $0 OFS "null"}' ogfile.csv > newfile.csv

but it appears to be adding a new row after every row, with the second column having a field of "null"

new rows instead of new column

can anyone help me understand why this is happening?

Upvotes: 1

Views: 380

Answers (1)

John1024
John1024

Reputation: 113834

Your source file has DOS/Windows line endings. When one sees anomalous output, this is a good first item to check. Two solutions:

  1. Use a utility such as dos2unix to remove the unwanted \r character from your input file. dos2unix is available on most distributions.

or,

  1. Modify your awk command to recognize and remove the offending characters:

    awk 'BEGIN{RS="\r\n"; FS=OFS=","}{print $0 OFS "null"}' ogfile.csv
    

Upvotes: 1

Related Questions