我在處理CSV的bash腳本中使用了一些awk。 awk的做到這一點:如何使用awk添加帶有標題的新列到csv
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
哪個做這樣的轉換:
# 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
我希望新列有一個頭,讓新的CSV看起來像
# 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
我知道有一種方法可以單獨指定在第一行中做什麼,但我一直無法弄清楚。
完美工作,謝謝。你可以在第二個解決方案中解釋''''「%s%s」'''? –
@dataprincess,您的歡迎,很高興它幫助你。我也爲代碼添加了解釋,如果您有任何疑問,請告訴我。 – RavinderSingh13