Comparing two files and dividing the values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing two files and dividing the values
# 1  
Old 10-01-2010
Comparing two files and dividing the values

Hi all,
I am new to unix and I am trying hard to get this requirement, but no luck.
I am trying to compare two cloumns in two files and if it matches, the last column in file1 must be divided by file2 and the output must be written in a new file. To elaborate the 2nd column in file1 (EUR) must be compared with 1st column in file2 (EUR) and if it matches the last coulmn of EURO rows in file1 must be divided with last column in file2 of EURO row and the output must be similar to file1 with calculated values. And if there is no match between file1 and file2 the file1 unmatched records must be as it is. This must be similar for all matching strings.
Please help, thanks in advance.
Eg:
Code:
(7,050.00)/0.73351=9611.32
(57,412.00)/0.98250=58434.60

File1:
Code:
0000001100 EUR 2008-02-04 Vendor Tooling (7,050.00)
0000001100 CHF 2008-03-20 Vendor Tooling (57,412.00)
0000001100 U.S. Dollar 2010-04-22 Vendor Tooling (63,203.00)

File2:
Code:
EUR   0.73351
CHF   0.98250
Output:
0000001100 EUR 2008-02-04 Vendor Tooling (9611.32)
0000001100 CHF 2008-03-20 Vendor Tooling (58434.60)
0000001100 U.S. Dollar 2010-04-22 Vendor Tooling (63,203.00)


Last edited by Scott; 10-02-2010 at 10:42 AM.. Reason: Please use code tags
# 2  
Old 10-01-2010
A couple of questions...

1) Is this a homework question?
2) Are you doing line number matches? That is, is line 1 of file1 always compared to line 1 of file2? Or are you instead doing, "if EUR appears in column 2 of any line of file 1 and in the first column of any line of 2, perform the given calculation on those lines, whatever their numbers may be"? Both can be done, but the first is considerably easier.

Last edited by treesloth; 10-01-2010 at 09:15 PM..
# 3  
Old 10-02-2010
thanks for the reply treesloth.

As I am new, I was searching for awk, grep and sed commands functionality, but not able to get how the calculation can be done.

please help.

I am trying to calculate the values in last column, if EUR appears in column 2 of any line of file 1 and in the first column of any line of 2, perform the given calculation on those lines, likewise for other currencies also and if there are no matches, those records must be as is in output file.

Thanks again treesloth
I am trying to calculate the numbers
# 4  
Old 10-02-2010
In that case, something like this may work:

Code:
#! /usr/bin/awk -f

BEGIN   {
        while ("cat file2" | getline)
                {
                currency[$1] = $NF
                }
        }

        {
        gsub(",","",$NF)
        }

currency[$2] != ""       {
        gsub("[()]","",$0)
        $NF = "(" $NF / currency[$2] ")"
        }

        {
        print
        }

To use this, you'll need to make sure that the first line points correctly to the location of your awk binary, and that set it to be executable. Also note that "file2" is used within the program. If that's not the actual name of your file, change the program accordingly. Then, run it:

Code:
$ ./compute.awk file1
0000001100 EUR 2008-02-04 Vendor Tooling (9611.32)
0000001100 CHF 2008-03-20 Vendor Tooling (58434.6)
0000001100 U.S. Dollar 2010-04-22 Vendor Tooling (63203.00)


Last edited by treesloth; 10-03-2010 at 10:25 PM.. Reason: Made output format uniform (no commas)
This User Gave Thanks to treesloth For This Post:
# 5  
Old 10-04-2010
Hi treesloth,
The code is not working in my server, however it is working on different server. Might be issue with awk binary.
Anyhow you are awesome. Thanks a lot for your timely help. I will go through the forum whenever i will get time to update my knowlede.

Srini

Last edited by smadderla; 10-04-2010 at 12:00 PM..
# 6  
Old 10-04-2010
I'm not exactly sure what would cause syntax errors in this script, but let's start with these:

1) All of the lines in the error use double quotes. Sometimes copying and pasting from a web page messes those up. You can go through and manually replace all double quotes.
2) Does the script have the user execute bit set? This seems unlikely to be the cause, really, since the expected error would be something more like "Permission denied", but we might as well check it.
3) What version of awk are you using? Do awk --version.
4) What version of Unix/Linux are you running?

---------- Post updated at 09:21 AM ---------- Previous update was at 09:13 AM ----------

Ah, just saw your last reply. If it's a question of the awk binary version, you might want to move this to the shell scripting section of the forums. There are some real wizards that hang out there that can probably tell you very quickly the little things that cause some versions of awk to choke. I'm afraid I only know what I come up with on my systems, generic though they may be.

Last edited by treesloth; 10-04-2010 at 12:19 PM..
This User Gave Thanks to treesloth For This Post:
# 7  
Old 10-06-2010
It seems the issue with awk binary version.

Once again thanks for your timely help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

2. Shell Programming and Scripting

Dividing values within rows with awk?

Hello everyone, I have a dataset that looks like: 1 2 3 4 5 6 I was wondering if was possible to divide values within rows: 1/2 3/4 5/6 I don't think using awk -F"\t" '{ print $1/$2 }' would work here, or would it? Thanks in advance! (2 Replies)
Discussion started by: Rabu
2 Replies

3. Shell Programming and Scripting

Comparing the values of two files

Hi Am trying to compare the values of two files.. One is a big file that has many values and the other is a small file.. The big file has all values present in small file.. # cat SmallFile 4456602 22347881 7471282 15859891 8257690 21954701 7078068 18219229 2883826 6094959 100000 ... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. Shell Programming and Scripting

Dividing one file into several files

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows, the first column represents the name of populations, in my real file, I have 47 populations and 2172 lines in total with about 47000 fields. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0 I want... (3 Replies)
Discussion started by: Homa
3 Replies

5. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

6. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

7. Shell Programming and Scripting

comparing values

i have two file and i am comparing both.. in cmp1 ,the content is : the nu of file is : <some integer value> in cmp2 ,the content is : the nu of file is : so want a script which will take value (2) when cmp1 is compared with cmp2.. i mean cmp cmp1 cmp2 the the output will be he nu of... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

8. Shell Programming and Scripting

comparing 2 text files to get unique values??

Hi all, I have got a problem while comparing 2 text files and the result should contains the unique values(Non repeatable). For eg: file1.txt 1 2 3 4 file2.txt 2 3 So after comaping the above 2 files I should get only 1 and 4 as the output. Pls help me out. (7 Replies)
Discussion started by: smarty86
7 Replies

9. Shell Programming and Scripting

comparing PID values of 2 Files in shell Options

Hi, There is a file having a list of running PIDs and another file having a list of registered PIDs. How can we check if each value of running PIDs are less or more than the registered PIDs i.e. comparing the value of each PID in both the files. Request you to pls give your inputs. ... (2 Replies)
Discussion started by: marconi
2 Replies

10. UNIX for Dummies Questions & Answers

Dividing float values

Hi I know its a dumb question but can any one please explain me the difference of executing a shell script in the following 2 ways. . script.sh sh script.sh I have a problem if I execute the following code as sh script.sh DB_CNT_ALW=0.20 SCT_VAR=0.05 if ; then echo "== Difference... (3 Replies)
Discussion started by: shash
3 Replies
Login or Register to Ask a Question