awk to skip header row and add string to field

The awk below does put in VUS in the 9th field but I can not seem to skip the header then add the VUS . I tried to incorporate NR >=2 and NR > 1 with no luck. Thank you :).

input

Chr    Start    End    Ref    Alt    Func.refGene    PopFreqMax    CLINSIG    Classification
chr1    43395635    43395635    C    T    exonic    0.12    Benign    
chr1    43396414    43396414    G    A    exonic    0.14    Benign    
chr1    172410967    172410967    G    A    exonic    0.66        
chr1    172411496    172411496    A    G    exonic    1        
chr2    51254901    51254901    G    A    exonic    0.48        
chr2    51254914    51254914    C    T    exonic    0.0023    

current output

Chr    Start    End    Ref    Alt    Func.refGene    PopFreqMax    CLINSIG    VUS
chr1    43395635    43395635    C    T    exonic    0.12    Benign    VUS
chr1    43396414    43396414    G    A    exonic    0.14    Benign    VUS
chr1    172410967    172410967    G    A    exonic    0.66        VUS
chr1    172411496    172411496    A    G    exonic    1        VUS
chr2    51254901    51254901    G    A    exonic    0.48        VUS
chr2    51254914    51254914    C    T    exonic    0.0023        VUS

desired output

Chr    Start    End    Ref    Alt    Func.refGene    PopFreqMax    CLINSIG    Classification
chr1    43395635    43395635    C    T    exonic    0.12    Benign    VUS
chr1    43396414    43396414    G    A    exonic    0.14    Benign    VUS
chr1    172410967    172410967    G    A    exonic    0.66        VUS
chr1    172411496    172411496    A    G    exonic    1        VUS
chr2    51254901    51254901    G    A    exonic    0.48        VUS
chr2    51254914    51254914    C    T    exonic    0.0023        VUS

awk

awk -v OFS='\t' '$9=$9"VUS"' input
awk -v OFS='\t' 'NR>1 {$9=$9"VUS"} 1' file
1 Like

Hello cmccabe,

Could you please try following, as here I am not hardcoding the number of fields and as per your shown Input_file and expected output, adding the field's value at last. So in case number of fields vary too into your Input_file it will provide same output.

awk 'BEGIN{OFS="\t"} NR>1{$(NF+1)="VUS"} 1'  Input_file

Output will be as follows.

Chr    Start    End    Ref    Alt    Func.refGene    PopFreqMax    CLINSIG    Classification
chr1    43395635        43395635        C       T       exonic  0.12    Benign  VUS
chr1    43396414        43396414        G       A       exonic  0.14    Benign  VUS
chr1    172410967       172410967       G       A       exonic  0.66    VUS
chr1    172411496       172411496       A       G       exonic  1       VUS
chr2    51254901        51254901        G       A       exonic  0.48    VUS
chr2    51254914        51254914        C       T       exonic  0.0023  VUS
 

Thanks,
R. Singh

1 Like

Thank you very much :).

---------- Post updated at 09:02 AM ---------- Previous update was at 08:58 AM ----------

@RavinderSingh13 using your approach NF+1 is the last field (Classification). What would PopFregMax NF-2 or Func.RefGene NF-3 be? Thank you :).

Hello cmccabe,

You could get all the fields values in 1st line by as follows.

awk 'NR==1{for(i=1;i<=NF;i++){print "Number of field in terms of NF is--> NF-" NF-i", value is-->" $i}}'   Input_file

Output will be as follows.

Number of field in terms of NF is--> NF-8, value is-->Chr
Number of field in terms of NF is--> NF-7, value is-->Start
Number of field in terms of NF is--> NF-6, value is-->End
Number of field in terms of NF is--> NF-5, value is-->Ref
Number of field in terms of NF is--> NF-4, value is-->Alt
Number of field in terms of NF is--> NF-3, value is-->Func.refGene
Number of field in terms of NF is--> NF-2, value is-->PopFreqMax
Number of field in terms of NF is--> NF-1, value is-->CLINSIG
Number of field in terms of NF is--> NF-0, value is-->Classification
 

Off course above code NF-0 means value of NF only.

Thanks,
R. Singh

1 Like

Thank you for your help and explanations, I really appreciate them :slight_smile: