如何使用awk将带有标题的新列添加到csv

dat*_*ess 5 csv bash awk

我在处理 CSV 的 bash 脚本中使用了一些 awk。awk 这样做:

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 
Run Code Online (Sandbox Code Playgroud)

哪个进行此转换:

# 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
Run Code Online (Sandbox Code Playgroud)

我希望新列有一个标题,以便新的 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
Run Code Online (Sandbox Code Playgroud)

我知道有一种方法可以单独指定第一行中的操作,但我一直无法弄清楚。

Rah*_*rma 5

使用sed

$ sed '1s/$/,\tcmpgn_group/; 2,$s/$/,\t2017_01/' file
Run Code Online (Sandbox Code Playgroud)

即 for 1st line: append,\tcmpgn_group
和 for 2 to $: append,\t2017_01

使用awk

$ awk -v d="2017_01" -F"," 'FNR==1{a="cmpgn_group"} FNR>1{a=d} {print $0",\t"a}' f1
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)


Rav*_*h13 4

遵循 awk(在您的解决方案中进行了一些更改)应该适合您。

ORIG_FILE="score_model.csv"   
NEW_FILE="updates/score_model.csv"    
awk -v d="2017_01" -F"," 'BEGIN {OFS = ","} FNR==1{$(NF+1)="cmpgn_group"} FNR>1{$(NF+1)=d;} 1' $ORIG_FILE > $NEW_FILE 
Run Code Online (Sandbox Code Playgroud)

解决方案二:或者让我们删除这个$(NF+1)(创建一个新的字段方法)并尝试直接打印它。

awk -v d="2017_01" -F"," 'BEGIN {OFS = ","} {printf("%s%s",$0,FNR>1?d RS:"cmpgn_group" RS)}' $ORIG_FILE > $NEW_FILE
Run Code Online (Sandbox Code Playgroud)

上述命令的解释:

awk -v d="2017_01" -F"," ' ##Setting valur of variable named d as 2017_01 and setting field separator as comma.
BEGIN{                     ##Starting BEGIN section of awk here.
  OFS = ","                ##Setting Output field separator as comma here.
}                          ##Closing BEGIN block here.
{
  printf("%s%s",$0,FNR>1?d RS:"cmpgn_group" RS) ##Using printf here to print the lines. So %s%s means to print 2 strings here. First I am simply printing $0(current line). Then while printing second string using condition FNR>1(when line number is greater than 1) then print variable d(which we want to add at last) with RS(to print a new line here). Else(if condition FNR>1 is not true) then it means it is very first line of Input_file and print string "cmpn_groups" with RS(record separator) whose default value is a new line.
}
' $ORIG_FILE > $NEW_FILE   ##Mentioning Input_file named #ORIG_FILE and redirecting it's output to $NEW_FILE here.
Run Code Online (Sandbox Code Playgroud)