Add new columns based on existing columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new columns based on existing columns
# 1  
Old 04-16-2015
Java Add new columns based on existing columns

Hi all,

I am kind of stuck with printing my desired output. Please help me if you know how it can work.

My input file(tab separated):

Code:
NW_0068.1  41,16   100,900
NW_0699.1  4,2,19  200,700,80

My Output file (desired):
Code:
NW_0068.1  41,16   100,900  100 - 141  
NW_0068.1  41,16   100,900   900 - 916
NW_0699.1  4,2,19  200,700,800   200 - 204
NW_0699.1  4,2,19  200,700,800    700 - 702
NW_0699.1  4,2,19  200,700,800     800 - 819

My current code :

Code:
awk 'BEGIN {FS = "\t" }
           {
            block_size = $2 ; split(block_size, b, ","); 
            start_pos = $3  ; split(start_pos, s, ",") ; 
                  {for (i in b)
                       ex_size = s[i] + b[i] 
                       print $0, s[i],"-",ex_size 
                  } 
           }' test_input.txt

My current output:
Code:
NW_0068.1  41,16   100,900 900 - 916
NW_0699.1  4,2,19  200,700,800 800 - 819

When I print value of "i" within the loop it does show 1,2 for first record and 1,2,3 for the second but only the last value is printed in the final output Smilie Any hints??

Also, it doesn't make any difference if I print the output inside or outside of for loop, why is this so?

Thanks!

Last edited by Scrutinizer; 04-16-2015 at 08:45 AM.. Reason: formating; [mod] CODE tags
# 2  
Old 04-16-2015
Please use code tags for data as well! In above, the TABS in the data are lost.

Your braces are misplaced. Try moving the one before for (i in b) just behind it.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-16-2015
In addition to that what RudiC wrote, the order of a for (variable in array) loop is not defined. Instead, you could try this:
Code:
awk '
       BEGIN {
           FS = OFS= "\t" 
       }
       {
           block_size = $2 ; m=split(block_size, b, ","); 
           start_pos = $3  ; split(start_pos, s, ",") ; 
           for (i=1; i<=m; i++) {
               ex_size = s[i] + b[i] 
               print $0, s[i],"-",ex_size 
           } 
       }
' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-20-2015
Many thanks for your feedback... RudiC and Scrutinizer!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Group by columns and add sum in new columns

Dear Experts, I have input file which is comma separated, has 4 columns like below, BRAND,COUNTRY,MODEL,COUNT NIKE,USA,DUMMY,5 NIKE,USA,ORIGINAL,10 PUMA,FRANCE,DUMMY,20 PUMA,FRANCE,ORIGINAL,15 ADIDAS,ITALY,DUMMY,50 ADIDAS,ITALY,ORIGINAL,50 SPIKE,CHINA,DUMMY,1O And expected output add... (2 Replies)
Discussion started by: ricky1991
2 Replies

2. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

3. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

4. Shell Programming and Scripting

How to add columns based on a pattern using awk?

Hi, I have a file with more than 1000 lines with ~14 columns. I need to find all the lines with matching value in column 14 and then add column 6 in all the lines before printing them out.. e.g if this is the input file: abc test input 10 for process 2345 abc test input 15 for process 2348... (1 Reply)
Discussion started by: xkdasari
1 Replies

5. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

6. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

7. UNIX for Dummies Questions & Answers

Add columns to an existing excel sheet

Hi All, i have an excel sheet as below: day ----- monday tuesday wenesday thursday friday i need to append the two more columns in this existing file below: insert date should be todays date--- day insert date insert user ---- ---------- ... (3 Replies)
Discussion started by: arunmanas
3 Replies

8. Shell Programming and Scripting

Awk to add columns from a file into an existing file

Hi! I would need some help to add the last two columns of one file into another file using awk (or something similar). For example, I have: file 1: file 2: car book day root lag bar look pay boot tag tar took may moot sag I want to have:... (5 Replies)
Discussion started by: coconaza
5 Replies

9. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

10. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies
Login or Register to Ask a Question