Searching columns and subtracting values in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching columns and subtracting values in awk
# 1  
Old 08-29-2012
Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex.

I have two files:

Code:
File 1:

0808 166 166 62 9 0
1000fights 1 1 2 1 0
100places2visit 2 2 2 2 0
10veronica91 167 167 3 1 0
11thgorgeous 346 346 3806 1461 122
12angelal 24 24 5 0 1
15minbeauty 1201 1201 2164 478 45
1happybecky 1318 1318 663 164 7
2bears 908 908 694 171 38
2cre8 125 125 305 93 8

File 2:

0808 195 195 20 0 104
0minali0 1 1 0 0 0
0p0g3kw 2 2 17 0 75
1000fights 1 1 4 0 5
100places2visit 17 17 40 0 111
10veronica91 178 178 1 0 476049835
11thgorgeous 450 450 3363 0 9115
12angelal 271 271 31 0 147
12sheeshee 6 6 7 0 38
159159159 3 3 6 0 13

Values for column 1 might be in file 1 and not file 2, or in file 2 and not file 1.

I want awk to look at file 1 and save those column 1 values,
then match those values with the corresponding row in file 2 and then calculate the differences between these numbers. so, for example, line 1's desired
output would be.

Code:
0808 29 29 -42 -9 -104

A couple of caveats. If a value for column 1 is not in both files, I don't care about it. Also, the solution doesn't have to be awk, or fast or efficient. I just need something that works!

Thanks so much.
# 2  
Old 08-29-2012
Code:
awk     'NR==FNR   {tmp[$1]=$0;
                    next
                   }
         $1 in tmp {split(tmp[$1],ci);
                    print $1, $2-ci[2], $3-ci[3], $4-ci[4], $5-ci[5], $6-ci[6]
                   }
        ' file1 file2

Your sample result should have 104 in the last column.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-29-2012
This works great, its exactly what I needed. Thanks so much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: compare values in two columns of the same file

I'm trying to learn awk, but I've hit a roadblock with this problem. I have a hierarchy stored in a file with 3 columns: id name parentID 4 D 2 2 B 1 3 C 1 1 A 5 I need to check if there are any values in column 3 that are not represented anywhere in column 1. I've tried this: awk '{arr;}... (7 Replies)
Discussion started by: kaktus
7 Replies

2. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

3. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

4. Shell Programming and Scripting

Subtracting values from variable

Legends, Please help me in , how do i subtract the variable values listed like below. the first value of orig should be subtracted from first value of prev and so on. san> echo $orig 346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195... (15 Replies)
Discussion started by: sdosanjh
15 Replies

5. UNIX for Dummies Questions & Answers

Subtracting values from 2 columns in a file

Hello, I have a file with 5 columns that looks like this: A1BG chr19 + 58863335 58866549 A1BG chr19 - 58858171 58864865 A2LD1 chr13 - 101182417 101186056 A2LD1 chr13 - 101182417 101241046 A2M chr12 - 9220303 9268558 A2ML1 ... (5 Replies)
Discussion started by: wolf_blue
5 Replies

6. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

7. Shell Programming and Scripting

Subtracting columns against each other

Hi All, I have a file of 100 lines of each having 1000 columns. I need to find the difference of each column against each other. That means, Col1-Col1; Col1-Col2; Col1-Col3;......Col1-Col1000; Col2-Col1; Col2-Col2; Col2-Col3;.... and so on ....up to Col1000-Col1000. Lets say the file is... (6 Replies)
Discussion started by: Fredrick
6 Replies

8. Shell Programming and Scripting

subtracting 1.5 from column using awk and saving the changes

# foreach sub ( sub001 ) sub=sub001 cd /mnt/stor/smith/recog/$sub/event_files/timecorrected/ awk '{$1-1.5}' $sub_CR end What im trying to do is: 1. open a 1D file that consists of lists of integers in rows a columns 2. subtract 1.5 from each integer in the first column 3. save the file... (1 Reply)
Discussion started by: ac130pilot
1 Replies

9. Shell Programming and Scripting

Subtracting with awk?

i have a small awk script which prints the 5 columns of different o/p i want the 5th column subtracted from 100 and then display the result .. but i do not get the desired result .. I 'm using following script awk ' BEGIN { FS="" RS="us" } { ... (3 Replies)
Discussion started by: fugitive
3 Replies

10. Shell Programming and Scripting

comparing files - adding/subtracting/formating columns

I have two files: file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt:... (7 Replies)
Discussion started by: oabdalla
7 Replies
Login or Register to Ask a Question