shell - Concatenate two columns and paste the merged column -


i have tab delimited file 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 

i concatenate 2 columns name1 , name2 (joined "__").the merged column should pasted new column "merged_names" in new file. how can using awk.

expected output -

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 

you can use awk:

awk 'begin{ofs=fs="\t"} nr==1{$(nf+1)="merged_names"} nr!=1{$(nf+1)=$(nf-1) "__" $nf}1' file 

more shortened awk:

awk 'begin{ofs=fs="\t"} {$(nf+1)=(nr==1)? "merged_names" : $(nf-1)"__"$nf}1' file 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -