Match two files and divide fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match two files and divide fields
# 1  
Old 12-27-2010
Question Match two files and divide fields

I have two files that have the date field in common. I request your help with some script that divide each field value from file1 by the correspond field value of the file2 only when the field date is equal in both files. Thanks in advance !

This is a sample of the files

file 1

12/16/2010, 134.21, 134.23, 132.86, 133.81, 23463000
12/17/2010, 133.97, 134.63, 133.22, 134.2, 17213000
12/20/2010, 135.13, 135.44, 134.33, 135.11, 13129500
12/21/2010, 135.17, 135.78, 134.79, 135.32, 10235600
12/22/2010, 135.6, 135.7, 134.97, 135.05, 7300600
12/23/2010, 134.26, 134.9, 133.93, 134.66, 8700800

file 2

12/16/2010, 123.53, 124.26, 123.10, 124.17, 186003814
12/17/2010, 124.08, 124.46, 123.82, 124.3, 141075300
12/20/2010, 124.64, 124.9, 123.98, 124.6, 119085500
12/21/2010, 124.99, 125.47, 124.87, 125.39, 94965500
12/22/2010, 125.48, 125.82, 125.41, 125.78, 78878100
12/23/2010, 125.64, 125.78, 125.29, 125.6, 70053700
# 2  
Old 12-27-2010
something like..

Code:
#! /usr/local/bin/bash
exec 6<f2
while IFS=, read v1 v2 v3 v4 v5 v6
do
        IFS=, read n1 n2 n3 n4 n5 n6 <&6
        if [ "$v1" = "$n1" ]; then
                echo $v1,$(echo "scale=2;$v2/$n2" | bc),$(echo "scale=2;$v3/$n3" | bc),$(echo "scale=2;$v4/$n4" | bc),$(echo "scale=2;$v5/$n5" | bc),$(echo "scale=2;$v6/$n6" | bc)
        fi
done < f1
6<&-

O/P for the given sample:
Code:
12/16/2010,1.08,1.08,1.07,1.07,.12
12/17/2010,1.07,1.08,1.07,1.07,.12
12/20/2010,1.08,1.08,1.08,1.08,.11
12/21/2010,1.08,1.08,1.07,1.07,.10
12/22/2010,1.08,1.07,1.07,1.07,.09

Note: I have changed the date for the last row to test and its ignored.

May be awk solution would be simpler.
This User Gave Thanks to clx For This Post:
# 3  
Old 12-27-2010
Assuming every record will have 6 fields:
Code:
awk -F, 'NR==FNR{a[$1]=$2;b[$1]=$3;c[$1]=$4;d[$1]=$5;e[$1]=$6;next;}{if(a[$1]) print $1", "$2/a[$1]", "$3/b[$1]", "$4/c[$1]", "$5/d[$1]", "$6/e[$1]; else print $0;}' file2 file1

This User Gave Thanks to anurag.singh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching two fields in two csv files, create new file and append match

I am trying to parse two csv files and make a match in one column then print the entire file to a new file and append an additional column that gives description from the match to the new file. If a match is not made, I would like to add "NA" to the end of the file Command that Ive been using... (6 Replies)
Discussion started by: dis0wned
6 Replies

2. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 Replies

3. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

4. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

7. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

8. Shell Programming and Scripting

how to match fields from different files in PERL

Howdy! I have multiple files with tab-separated data: File1_filtered.txt gnl|Amel_4.0|Group3.29 1 G R 42 42 60 15 ,.AAA.aa,aa.A.. hh00/f//hD/h/hh gnl|Amel_4.0|Group3.29 2 C Y 36 36 60 5 T.,T, LggJh gnl|Amel_4.0|Group3.29 3 A R 27 27 60 9 Gg,,.gg., B6hcc22_c File2_filtered.txt ... (3 Replies)
Discussion started by: sramirez
3 Replies

9. UNIX for Dummies Questions & Answers

Match values from 2 files and append certain fields

Hi, I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly. ... (1 Reply)
Discussion started by: redse171
1 Replies

10. Shell Programming and Scripting

Match two files and divide a field !

Hello, I have two files that have the date field as a common. I request your help with some script that divide the value of the file1 by the value in the file2 only when the field date are the same between both files and create a new text file. This is a sample of the files file1... (1 Reply)
Discussion started by: csierra
1 Replies
Login or Register to Ask a Question