2017-10-06 1 views
1

CSV를 처리하는 bash 스크립트에서 awk를 사용하고 있습니다. AWK이 수행 : 내가 아는 awk로 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 

같은 새로운 CSV가 보이도록
# 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 

나는, 새로운 열 헤더를 갖고 싶어 :이 변환을 수행

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 

별도로 첫 번째 행에서 수행 할 작업을 지정하는 방법이 있지만이를 이해할 수는 없습니다.

답변

1

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 

해결 방법 2 : 또는의이 $(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 

설명 :

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. 
+1

가, 감사합니다. 두 번째 해법에서''' "% s % s"'''을 설명 할 수 있습니까? –

+0

@dataprincess, 환영합니다. 다행히 도움이되었습니다. 나는 코드에 대해서도 설명을 추가했다. 친절하게도 당신이 어떤 질문이 있으면 알려줘. – RavinderSingh13

3
awk -v d="2017_01" 'BEGIN{FS=OFS=","} {print $0, (NR>1?d:"cmpgn_group")}' file 
0

을 사용하여이

$ sed '1s/$/,\tcmpgn_group/; 2,$s/$/,\t2017_01/' file 

1st line을위한 즉 나오지 : ,\tcmpgn_group 추가
2 to $을 위해 : AWK를 사용 ,\t2017_01

를 추가

$ awk -v d="2017_01" -F"," 'FNR==1{a="cmpgn_group"} FNR>1{a=d} {print $0",\t"a}' f1 

출력 : 완벽하게 작동

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