awk to skip header row and add string to field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to skip header row and add string to field
# 1  
Old 08-01-2016
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 Smilie.

input
Code:
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
Code:
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
Code:
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
Code:
awk -v OFS='\t' '$9=$9"VUS"' input


Last edited by cmccabe; 08-01-2016 at 06:10 PM.. Reason: fixed format
# 2  
Old 08-01-2016
Code:
awk -v OFS='\t' 'NR>1 {$9=$9"VUS"} 1' file

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-02-2016
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.
Code:
awk 'BEGIN{OFS="\t"} NR>1{$(NF+1)="VUS"} 1'  Input_file

Output will be as follows.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 08-02-2016
Thank you very much Smilie.

---------- 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 Smilie.

Last edited by cmccabe; 08-02-2016 at 11:03 AM.. Reason: fixed format
# 5  
Old 08-02-2016
Quote:
Originally Posted by cmccabe
Thank you very much Smilie.
---------- 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 Smilie.
Hello cmccabe,

You could get all the fields values in 1st line by as follows.
Code:
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-02-2016
Thank you for your help and explanations, I really appreciate them Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add field header and default value in awk

I am trying to add a field header Inheritence in between $9 and $10 and default the value of each line to .. The below awk is close I think. Thanks :). input R_Index Chr Start End Ref Alt Func.IDP.refGene Gene.IDP.refGene ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

At text to field 1 of header row using awk

I am just trying to insert the word "Index" using awk. The below is close but seems to add the word at the end and I can not get the syntax correct to add from the beginning. Thank you :). awk -F'\t' -v OFS='\t' '{ $-1=$-1 OFS "Index"}$1=$1' file current output Chr Start End ... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

6. Shell Programming and Scripting

awk transpose row into 2 field column

Need to transpose every 2 fields of a row into a single 2 field column. input 4 135 114 76 217 30 346 110 5 185 115 45 218 85 347 125 6 85 116 130 220 65 352 95 11 30 117 55 221 42 355 75 16 72 118 55 224 37 357 430 17 30 119 55 225 40 358 62 21 52 120 65 232 480 360 180 ....... (8 Replies)
Discussion started by: sdf
8 Replies

7. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

8. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

9. Shell Programming and Scripting

help with sed to add delimiter and new field to each row

I have a file with millions of rows that I need to add a delimiter and a new field with a zero to the end of each row. (its too big to open and do a find and replace regex) I'm looking for the next line '\n' and need to replace it with a Unit Separator (hex \037) 0 \n. I've tried the... (2 Replies)
Discussion started by: kmac
2 Replies

10. Shell Programming and Scripting

Skip parsing the header record - Awk

Guys.... Got a scenario in which I need to skip parsing the header record while I do an awk. Does awk has the flexibility to accomplish this?. If so, how do we do this?. Thanks !!! -Anduzzi :) (2 Replies)
Discussion started by: anduzzi
2 Replies
Login or Register to Ask a Question