如何使用awk将带有标题的新列添加到csv [英] How to add new column with header to csv with awk

查看:276
本文介绍了如何使用awk将带有标题的新列添加到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 

进行以下转换:

# 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看起来像

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.

推荐答案

遵循awk(您的解决方案有所更改)应该对您有用.

Following awk(a bit changed in your solution) should work for you.

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 

解决方案2nd: ,或者让我们删除此$(NF+1)(创建新的现场方法),然后尝试直接打印它.

Solution 2nd: Or let's remove this $(NF+1)(creating a new field approach) and try to directly print it.

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

上述命令的说明:

Explanation of above command:

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.

这篇关于如何使用awk将带有标题的新列添加到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆