Match two files and divide a field !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match two files and divide a field !
# 1  
Old 06-19-2010
Question 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
Code:
6/1/2010,    107.02
6/3/2010,    110.19
6/4/2010,    106.31
6/7/2010,    104.99
6/8/2010,    106.12
6/9/2010,    105.55
6/11/2010,    109.16
6/14/2010,    108.99
6/15/2010,    111.47
6/16/2010,    111.43
6/17/2010,    111.61
6/18/2010,    111.73

file2
Code:
6/1/2010,    19.91
6/2/2010,    19.78
6/3/2010,    17.96
6/4/2010,    19.19
6/8/2010,    21
6/9/2010,    20.56
6/10/2010,    18.97
6/11/2010,    20.01
6/15/2010,    20.99
6/16/2010,    20.33
6/17/2010,    21.9
6/18/2010,    22.83

Thanks in advance

Last edited by Scott; 06-20-2010 at 08:15 AM..
# 2  
Old 06-19-2010
Hi

Code:
awk 'NR==FNR{a[$1]=$2;next;}{if ($1 in a) print $1, $2/a[$1];else print;}' file2 file1

For those dates in file1 which does not have a matching date in file2, the above will print the line in file1 as is. If that is not desired, remove the 'else print' part in the above.

To put the output in a new file, redirect the output of the above to a new file.

Guru.
This User Gave Thanks to guruprasadpr 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

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

4. Shell Programming and Scripting

Divide an EBCDIC files into multiple files based on value at 45-46 bytes

Hi All, I do have an EBCDIC file sent from the z/os , this file has records with different record types in it, the type of record is identified by bytes 45-46 like value 12 has employee record value 14 has salaray record and etc.... we do now want to split the big ebcdic file into multiple... (3 Replies)
Discussion started by: okkadu
3 Replies

5. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. Shell Programming and Scripting

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,... (2 Replies)
Discussion started by: csierra
2 Replies

7. Shell Programming and Scripting

Divide large data files into smaller files

Hello everyone! I have 2 types of files in the following format: 1) *.fa >1234 ...some text... >2345 ...some text... >3456 ...some text... . . . . 2) *.info >1234 (7 Replies)
Discussion started by: ad23
7 Replies

8. Shell Programming and Scripting

Script to Match first field of two Files and print

Hi, I want to get script or command in Sun Unix which matches first fields of both the files and print the feilds of one files, example may make it more clear. InputFile1 ================== Alex,1,fgh Menthos,45454,toto Gothica,855,ee Zenie4,77,gg Salvatore,66,oo Dhin,1234,papapa... (3 Replies)
Discussion started by: indian.ace
3 Replies

9. Shell Programming and Scripting

match field between 2 files

I would like to do the following in bash shell. file a a:1 b:2 c:3 file b a:work:apple b:baby:banana c:candy:cat d:desk:dog I would like to match field 1 in file a to file b, if there's a match I would like to append field 2 in file a to field 3 in file b. Thank you. (8 Replies)
Discussion started by: phamp008
8 Replies

10. Shell Programming and Scripting

divide field in two strings

Hi everybody: Anybody know how could I divide a field like this: 1234 like 12 34. thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies
Login or Register to Ask a Question