user15889165
user15889165

Reputation: 11

AWK command to input new csv column

I have a .csv that has two columns already. I have a percentage.txt file with 1 column of data which I would like to input as a whole column into the csv @ column 3..

I cannot seem to work out a command ..

This is what I have and is not working

awk -F, '{getline f1 <"dels_20210508.csv" ;print f1,$3}' OFS=, percent.txt

My desired output would be to the original csv would be the following (col3-data, will come from the percent.txt first column)

col1-data col2-data col3-data
col1-data col2-data col3-data
col1-data col2-data col3-data
col1-data col2-data col3-data

Upvotes: 1

Views: 199

Answers (1)

peak
peak

Reputation: 116700

Unless percentage.txt is a valid one-column CSV file, you would have to make it "CSV-safe" before simply pasting it to the CSV file. Since you included as a tag, it may be of interest to show how this could be done with jq.

Assuming the .csv file is valid CSV and that percentage.txt and the .csv file are properly matched w.r.t. headers, you could use jq and paste together, along the lines of:

paste -d, dels_20210508.csv <(jq -Rr '[.]|@csv' percent.txt)

Upvotes: 1

Related Questions