Add field header and default value in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add field header and default value in awk
# 1  
Old 08-09-2016
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 Smilie.

input
Code:
   
R_Index         Chr         Start         End         Ref         Alt         Func.IDP.refGene         Gene.IDP.refGene         GeneDetail.IDP.refGene         ExonicFunc.IDP.refGene                 
 1         chr1         949654         949654         A         G         exonic         ISG15         0         synonymous SNV

desired result
Code:
  
R_Index         Chr         Start         End         Ref         Alt         Func.IDP.refGene         Gene.IDP.refGene         GeneDetail.IDP.refGene         Inheritence     ExonicFunc.IDP.refGene
   1         chr1         949654         949654         A         G         exonic         ISG15         0         .     synonymous SNV

awk
Code:
awk -F'\t' -v OFS='\t' '{$0=((NR==10) ? "Inheritence" : (NR - 1)) OFS $0} 1' input > newfile


Last edited by cmccabe; 08-09-2016 at 12:54 PM.. Reason: fixed format
# 2  
Old 08-09-2016
Hello cmccabe,

I am not that sure about your all requirements, specially about placing the DOT one(Please re-phrase your that requirement). So for adding a string into 10th field you could try following.
Code:
awk 'NR==1{$10="Inheritence" OFS $10} 1' OFS="\t\t" Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-09-2016
Sorry, after the Inheritence field is added each line under it would have a . in it as a default value.

Code:
xxxx Inheritence xxxxx
1     .     xxxx
2     .     xxxx
3     .     xxxx

Thank you Smilie

Last edited by cmccabe; 08-09-2016 at 01:03 PM.. Reason: fixed format
# 4  
Old 08-09-2016
Quote:
Originally Posted by cmccabe
Sorry, after the Inheritence field is added each line under it would have a . in it as a default value.

Code:
xxxx Inheritence xxxxx
1     .     xxxx
2     .     xxxx
3     .     xxxx

Thank you Smilie
Hello cmccabe,

Could you please try following.(haven't test though)
Code:
 awk '{$10=NR==1?"Inheritence" OFS $10:"." OFS $10} 1' OFS="\t\t" Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 08-09-2016
works perfect.... thank you very much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to add symbol to specific field

Trying to use awk to add a . to $4. The input and output is tab-delimeted, but the awk seems to add a . in front of $5 and is space-delimeted. It seems close, but I am not able to produce the desired output. Thank you :). file chr1 948895 949015 chr1:948895-949015 ISG15 chr1 ... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

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 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

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

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

7. Shell Programming and Scripting

awk : Filter a set of data to parse header line and last field of multiple same match.

Hi Experts, I have a data with multiple entry , I want to filter PKG= & the last column "00060110" or "00088150" in the output file: ############################################################################################### PKG= P8SDB :: VGS = vgP8SOra vgP8SDB1 vgP8S001... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

9. Shell Programming and Scripting

awk field equal something, then add something to the field

Hi Everyone, a.txt a b c 1 e e e e e a b c 2 e e e e e the output is a b c 1 e e e e e a 00b c 2 e e e e e when 4th field = '2', then add '00' in the front of 2nd field value. Thanks (9 Replies)
Discussion started by: jimmy_y
9 Replies

10. Shell Programming and Scripting

awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this : 4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00 4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00 4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00 As you can see the column n. 7 is a timestamp column, I need... (23 Replies)
Discussion started by: capnino
23 Replies
Login or Register to Ask a Question