awk Match First Field and Replace Second Column

Hi Friends,

I have looked around the forums and over online but couldn't figure out how to deal with this problem

input.txt

gene1,axis1/0/1,axis2/0/1			
gene1,axis1/1/2,axis2/1/2			
gene1,axis1/2/3,axis2/2/3			
gene2,axis1/3/4,axis2/3/4

Match on first column and if first column is same, then change the second column value to the first read value in that column. So, my output becomes

output.txt

gene1,axis1/0/1,axis2/0/1			
gene1,axis1/0/1,axis2/1/2			
gene1,axis1/0/1,axis2/2/3			
gene2,axis1/3/4,axis2/3/4

Thanks in advance.

If file is sorted you can try this

$ awk -F, 'NR!=1 && p==$1{$2=s}{p=$1;s=$2}1' OFS=\, file

gene1,axis1/0/1,axis2/0/1            
gene1,axis1/0/1,axis2/1/2            
gene1,axis1/0/1,axis2/2/3            
gene2,axis1/3/4,axis2/3/4

OR

if file is not sorted reading same file twice

$ awk -F, 'FNR==NR{if(!A[$1]++)X[$1]=$2;next}X[$1]{$2=X[$1]}1' OFS=\, file file

gene1,axis1/0/1,axis2/0/1            
gene1,axis1/0/1,axis2/1/2            
gene1,axis1/0/1,axis2/2/3            
gene2,axis1/3/4,axis2/3/4
1 Like