Generating multiple new columns with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generating multiple new columns with awk
# 1  
Old 02-26-2012
Generating multiple new columns with awk

Hi,

I'm trying to reformat a file to create a new columns reflecting the previous 2 over and over. By that I mean currently each observation has two columns and I want to create a third which has a value equal to 1 minus the sum of the previous two.

This is slightly complicated as 1) I require the new column to have the same header/first row as the previous two 2) I want to skip the first three columns 3) I want to do this to the end of the line, for files with different line lengths (numbers of observations)

Just to make it clearer, this would be an example input file with just a header and one row:

Code:
Var1 Var2 Var3 ID1 ID1 ID2 ID2.... IDn IDn
Var1 Var2 Var3 0.1 0.8 0.2  0.3.... x    y

And the output would look something like this:

Code:
Var1 Var2 Var3 ID1 ID1 ID1 ID2 ID2 ID2.... IDn IDn IDn
Var1 Var2 Var3 0.1 0.8 0.1  0.2 0.3 0.5....  x   y   (1-x-y)

I imagine the solution is something similar to that described in the topic "awk or python to join alternating columns" (sorry cant post urls yet) but I'm afraid I don't have the know-how to make all the changes I need!

Any help would be greatly appreciated!

Last edited by joeyg; 02-26-2012 at 06:55 PM.. Reason: Please wrap code and samples with CodeTags - makes easier to identify
# 2  
Old 02-26-2012
Try:
Code:
awk 'NR==1{for (i=4;i<=NF;i+=2) $(i+1)=$i" "$i}NR>1{for (i=4;i<=NF;i+=2) $(i+1)=$(i+1)" "(1-$i-$(i+1))}1' file


Last edited by bartus11; 02-26-2012 at 05:23 PM.. Reason: Shorter a bit
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-26-2012
How about this:

Code:
awk '{printf $1" "$2" "$3}
NR==1 { for(i=4;i<=NF;i++) {
   printf " " $i;
   if(i%2) printf " "$i }}
NR>1 { for(i=4;i<=NF;i++) {
   printf " " $i;
   if (i%2) printf " "1.0-$i-$(i-1)}}
{print ""}' infile

or this
Code:
awk '{printf $1" "$2" "$3}
{ for(i=4;i<=NF;i++) {
   printf " " $i;
   if(i%2) printf " " (NR==1?$i:1.0-$i-$(i-1))
}}
{print ""}' infile

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 02-27-2012
Thanks to you both! I only tried bartus11's response and it seems to have worked perfectly!

---------- Post updated at 06:28 PM ---------- Previous update was at 10:58 AM ----------

Sorry one last request as I seem to be suffering from rounding errors. Is it possible to incorporate a rule into the commands suggested that if the newly generated columns are negative (i.e. the sums of the two preceding columns used to generate the value are greater than one) they should be set instead to zero?

Thanks again!
# 5  
Old 02-28-2012
Code:
awk 'NR==1{for (i=4;i<=NF;i+=2) $(i+1)=$i" "$i}NR>1{for (i=4;i<=NF;i+=2) {x=1-$i-$(i+1);$(i+1)=$(i+1)" "(x>0?x:0)}}1' file

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 02-28-2012
Code:
awk '{for (i=4;i<=NF;i+=2) {x=1-$i-$(i+1);$(i+1)=$(i+1)" "(NR==1?$i:(x>0?x:0))}}1' file

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-01-2012
Thanks again to you both! Very much appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Average across multiple columns - awk

Hi forum members, I'm trying to get an average of multiple columns in a csv file using awk. A small example of my input data is as follows: cu,u3o8,au,ag -9,20,-9,3.6 0.005,30,-9,-9 0.005,50,10,3.44 0.021,-9,8,3.35 The following code seems to do most of what I want gawk -F","... (6 Replies)
Discussion started by: theflamingmoe
6 Replies

2. Shell Programming and Scripting

Comparing multiple columns using awk

Hello All; I have two files with below conditions: 1. Entries in file A is missing in file B (primary is field 1) 2. Entries in file B is missing in file A (primary is field 1) 3. Field 1 is present in both files but Field 2 is different. Example Content: File A ... (4 Replies)
Discussion started by: mystition
4 Replies

3. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

4. Shell Programming and Scripting

Sort and join multiple columns using awk

Is it possible to join all the values after sorting them based on 1st column key and replace empty rows with 0 like below ? input a1 0 a1 1 a1 1 a3 1 b2 1 a2 1 a4 1 a2 1 a4 1 c4 1 a3 1 d1 1 a3 1 b1 1 d1 1 a4 1 c4 1 b2 1 b1 1 b2 1 c4 1 d1 1 output... (8 Replies)
Discussion started by: quincyjones
8 Replies

5. Shell Programming and Scripting

Extracting multiple columns with awk

Hi everyone!! I need to apply a simple command to extract columns from a matrix, but I need to extract contemporary from the first to the tenth columns, than from the eleventh to the twentyth and so on... how can i do that? (1 Reply)
Discussion started by: gabrysfe
1 Replies

6. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Shell Programming and Scripting

Awk if-else syntax with multiple columns

I can't seem to get this to work. I can reformat the date field if it's the first field (and only field) in the file: However, I get a syntax error when the date field is the second field (or has any other columns following): I can use a ";" but then it puts each column on separate... (8 Replies)
Discussion started by: giannicello
8 Replies

8. UNIX for Dummies Questions & Answers

Generating different columns from same file

Hi, Our requirement is we have to create file from one file where all the rows from source file is converted into columns of the target file. For example Source file is : Status Report ABC Generated: 2009-05-05 08:49:47 Job start time=2009-05-05 08:43:43 Job end time=2009-05-05... (1 Reply)
Discussion started by: Amey Joshi
1 Replies

9. Shell Programming and Scripting

AWK subtraction in multiple columns

AWK subtraction in multiple columns Hi there, Can not get the following: input: 34523 934 9485 3847 394 3847 3456 9384 awk 'NR==1 {for (i = 1; i <= NF; i++) {n=$i; next}; {n-=$i} END {print n}' input output: 21188 first column only,... (2 Replies)
Discussion started by: awkward
2 Replies

10. Shell Programming and Scripting

extracting multiple consecutive columns using awk

Hello, I have a matrix 200*10,000 and I need to extract the columns between 40 and 77. I dont want to write in awk all the columns. eg: awk '{print $40, $41, $42,$43 ... $77}'. I think should exist a better way to do this. (10 Replies)
Discussion started by: auratus42
10 Replies
Login or Register to Ask a Question