awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ; , to $8 , if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output without changing file to the desired output. Thank you :).

file tab-delimeted

R_Index	Chr	Start	Func.refGene	Gene.refGene	GeneDetail.refGene	ExonicFunc.refGene	AAChange.refGene
1	chr1	17313157	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?	.	.
2	chr1	17313157	exonic	ATP13A2	.	nonsynonymous SNV	ATP13A2:NM_001141974:exon27:c.3214G>A:p.Ala1072Thr
3	chr1	17313165	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?	.	.

awk

awk 'BEGIN {OFS=FS="\t"} {if ($8==".")sub($8,$6); print }' file

desired output tab-delimeted

R_Index	Chr	Start	Func.refGene	Gene.refGene	GeneDetail.refGene	ExonicFunc.refGene	AAChange.refGene
1	chr1	17313157	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?	.	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?
2	chr1	17313157	exonic	ATP13A2	.	nonsynonymous SNV	ATP13A2:NM_001141974:exon27:c.3214G>A:p.Ala1072Thr
3	chr1	17313165	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?	.	NM_001141973:exon28:c.3221-30C>T:p=?
awk 'BEGIN {OFS=FS="\t"} {if ($8==".") $8=$6; print }' file
1 Like

In addition to what rdrtx1 already suggested, we could try something a little closer to what you were doing with sub() :

awk 'BEGIN {OFS=FS="\t"} {sub(/^\.$/,$6,$8); print}' file

but this will only work if $6 does not expand to a string containing any ampersand ( & ) characters. (I.e., it works with your sample data, but is dangerous if you don't know what characters might appear in field 6 in data your script might process later.) One could also try the slightly simpler:

awk 'BEGIN {OFS=FS="\t"} $8=="." {$8=$6} 1' file
1 Like

Thank you both very much :slight_smile: