I'm using some awk inside a bash script that's handling CSVs. The awk does this:
ORIG_FILE="score_model.csv"   
NEW_FILE="updates/score_model.csv"    
awk -v d="2017_01" -F"," 'BEGIN {OFS = ","} {$(NF+1)=d; print}' $ORIG_FILE > $NEW_FILE 
Which does this transformation:
# before
model_description,      type,    effective_date, end_date
Inc <= 40K,             Retired, 08/05/2016,     07/31/2017
Inc > 40K Age <= 55 V5, Retired, 04/30/2016,     07/31/2017
Inc > 40K Age > 55 V5 , Retired, 04/30/2016,     07/31/2017
# after, bad
model_description,      type,    effective_date, end_date,   2017_01  
Inc <= 40K,             Retired, 08/05/2016,     07/31/2017, 2017_01
Inc > 40K Age <= 55 V5, Retired, 04/30/2016,     07/31/2017, 2017_01
Inc > 40K Age > 55 V5 , Retired, 04/30/2016,     07/31/2017, 2017_01
I want the new column to have a header, so that the new CSV looks like
# after, desired
model_description,      type,    effective_date, end_date,   cmpgn_group  
Inc <= 40K,             Retired, 08/05/2016,     07/31/2017, 2017_01
Inc > 40K Age <= 55 V5, Retired, 04/30/2016,     07/31/2017, 2017_01
Inc > 40K Age > 55 V5 , Retired, 04/30/2016,     07/31/2017, 2017_01
I know there's a way to specify what to do in the first row separately, but I haven't been able to figure it out.
using sed
$ sed '1s/$/,\tcmpgn_group/; 2,$s/$/,\t2017_01/' file
i.e for 1st line : append ,\tcmpgn_group
and for 2 to $ : append ,\t2017_01
using awk
$ awk -v d="2017_01" -F"," 'FNR==1{a="cmpgn_group"} FNR>1{a=d} {print $0",\t"a}' f1
Output:
model_description,      type,    effective_date, end_date,      cmpgn_group
Inc <= 40K,             Retired, 08/05/2016,     07/31/2017,    2017_01
Inc > 40K Age <= 55 V5, Retired, 04/30/2016,     07/31/2017,    2017_01
Inc > 40K Age > 55 V5 , Retired, 04/30/2016,     07/31/2017,    2017_01
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With