2016-08-08 4 views
0

내가 ("__"에 의해 결합) NAME1 및 NAME2 두 개의 열을 연결하고자하는 follows-연결하여 두 개의 열 및 병합 된 열을 붙여

loci1 loci2 name1 name2 
    utr3p utr3p TERF1 ISCA2 
    utr3p intron LPP PAAF1 
    utr3p intron RPL37A RCC1 
    coding intron BAG2 RP11 
    intron intron KIF1B SNORA21 
    intron downstream GUSBP4 CTD 
    intron intron CLTC VMP1 
    utr3p utr3p PCYT1A ZHX3 

같은 탭으로 구분 된 파일이 있습니다. 병합 열은해야한다 새 파일에서 새 열 "merged_names"로 붙여 넣습니다. awk을 사용하여 어떻게 할 수 있습니까?

예상 출력 -

loci1 loci2 name1 name2 merged_names 
utr3p utr3p TERF1 ISCA2 TERF1__ISCA2 
utr3p intron LPP PAAF1 LPP__PAAF1 
utr3p intron RPL37A RCC1 RPL37A__RCC1 
coding intron BAG2 RP11 BAG2__RP11 
intron intron KIF1B SNORA21 KIF1B__SNORA21 
intron downstream GUSBP4 CTD GUSBP4__CTD 
intron intron CLTC VMP1 CLTC__VMP1 
utr3p utr3p PCYT1A ZHX3 PCYT1A__ZHX3 
+0

숙제를? – ams

답변

2

당신이 awk를 사용할 수 있습니다

awk 'BEGIN{OFS=FS="\t"} NR==1{$(NF+1)="merged_names"} NR!=1{$(NF+1)=$(NF-1) "__" $NF}1' file 

awk 단축 :

awk 'BEGIN{OFS=FS="\t"} {$(NF+1)=(NR==1)? "merged_names" : $(NF-1)"__"$NF}1' file 
+0

NF, NR을 사용하는 데 유용한 팁. 감사 – panbar

2
awk 'BEGIN{OFS="\t"; print "loci1 loci2 name1 name2 MERGED__NAMES"} {print $1,$2,$3,$4,$3 "__" $4}' infile 
loci1 loci2 name1 name2 MERGED__NAMES 
loci1 loci2 name1 name2 name1__name2 
utr3p utr3p TERF1 ISCA2 TERF1__ISCA2 
utr3p intron LPP  PAAF1 LPP__PAAF1 
utr3p intron RPL37A RCC1 RPL37A__RCC1 
coding intron BAG2 RP11 BAG2__RP11 
intron intron KIF1B SNORA21 KIF1B__SNORA21 
intron downstream  GUSBP4 CTD  GUSBP4__CTD 
intron intron CLTC VMP1 CLTC__VMP1 
utr3p utr3p PCYT1A ZHX3 PCYT1A__ZHX3 
+0

인쇄물로 긴 목록을 어떻게 든 피할 수 있는지 궁금합니다. ++ 어쨌든 .. 인쇄하는 동안 문자열을 연결 했으므로 참고할 가치가 있습니다. – sjsam