multiplication of two files based on the content of the first column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiplication of two files based on the content of the first column
# 1  
Old 11-24-2011
multiplication of two files based on the content of the first column

Hi,
This is something that probably it is more difficult to explain than to do.

I have two files e.g.

FILE1

Code:
A15    8.3102E+00 3.2000E-04
A15    8.5688E+00 4.3000E-05
B13    5.1100E-01 1.9960E+00
B16    5.1100E-01 2.3000E-03
B16    8.6770E-01 1.0000E-07
B16    9.8693E-01 3.4000E-05
C19m  1.1485E+00 5.4000E-06
C19m  1.2357E+00 1.6000E-04
...

FILE2

Code:
A15	1.648980E-18
B13	0.000000E+00
B16	2.666140E-28
C19m    0.000000E+00

What I would like to get is a FILE3 that contains:
- the first column of FILE1
- the 2nd column of FILE1
- the product of the 3rd column of FILE1 and the value in the 2nd column of FILE2 whose first columns matches the value of the first column of FILE1

I would like to include in the FILE 3 only the values that in the 3rd columns are greater than a certain value (e.g. 0.00).

In the case above it would be

Code:
A15  8.3102E+00  5.27674E-22
A15  8.5688E+00  7.09061E-23
B16  5.1100E-01  6.13212E-31
B16  8.6770E-01  2.66614E-35
B16  9.8693E-01  9.06488E-33

I'm not sure if it is clear. If not let me know.
Thanks for your help.
# 2  
Old 11-24-2011
Try this...
Code:
awk 'NR==FNR{a[$1]=$2;next} $1 in a{v=$3*a[$1]}v{print $1,$2,v}' file2 file1 > file3

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 11-24-2011
Quote:
Originally Posted by ahamed101
Try this...
Code:
awk 'NR==FNR{a[$1]=$2;next} $1 in a{v=$3*a[$1]}v{print $1,$2,v}' file2 file1 > file3

--ahamed
Spectacular,
-is there a way to specify the condition that $3*a[$1] must be greater that a certain amount e.g. 1.0E-10?
- how can I print out the file in a fixed format e.g. scientific with 5 significant digits?
# 4  
Old 11-24-2011
Try this...
Code:
awk 'NR==FNR{a[$1]=$2;next} $1 in a{v=$3*a[$1]}v>6e-31{printf("%s %.5f %s\n",$1,$2,v)}' file2 file1 > file3

The product i.e. $3*a[$1] is too small, so .5f will not print anything
--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 12-01-2011
Hello again,
this works only if all the element in the first column of file2 are present in the first column of file1.
If file2 is only

Code:
B13	0.000000E+00
B16	2.666140E-28
A15	1.648980E-18

I get

Code:
A15 8.3102E+00 5.27674e-22
A15 8.5688E+00 7.09061e-23
B16 5.1100E-01 6.13212e-31
B16 8.6770E-01 2.66614e-35
B16 9.8693E-01 9.06488e-33
C19m 1.1485E+00 9.06488e-33
C19m 1.2357E+00 9.06488e-33

Is there a way to set to obtain only

Code:
A15 8.3102E+00 5.27674e-22
A15 8.5688E+00 7.09061e-23
B16 5.1100E-01 6.13212e-31
B16 8.6770E-01 2.66614e-35
B16 9.8693E-01 9.06488e-33

Thank you very much in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

2. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

3. Shell Programming and Scripting

Multiplication of a column from 2 files using awk

Hi, I have two files (file1.txt, file2.txt) in which, I would like to multiply all the values in file1 with the first row value of file2, file1 * second row value of file2, file1 * third row value of file2 and so on. Below are my sample data. file1.txt file2.txt ... (6 Replies)
Discussion started by: ida1215
6 Replies

4. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

5. Shell Programming and Scripting

Help with analysis data based on particular column content

Input file: Total_counts 1306726155 100% Number_of_count_true 855020282 Number_of_count_true_1 160014283 Number_of_count_true_2 44002825 Number_of_count_true_3 18098424 Number_of_count_true_4 24693745 Number_of_count_false 115421870 Number_of_count_true 51048447 Total_number_of_false ... (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. UNIX for Advanced & Expert Users

Removing files based on name and content

Consider i have 2 directories a1 and a2. under a1, i have below files test1 test2 test3. Under a2,i have below files. test1 test2 test3 test4 test5My requirement is i will pass the directory names(2 parameters) and directory in which files needs to be removed.(3rd parameter) a)first... (11 Replies)
Discussion started by: pandeesh
11 Replies

7. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (1 Reply)
Discussion started by: perl_beginner
1 Replies

8. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (2 Replies)
Discussion started by: perl_beginner
2 Replies

9. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

10. Shell Programming and Scripting

Compare files column to column based on keys

Here is my situation. I need to compare two tab separated files (diff is not useful since there could be known difference between files). I have found similar posts , but not fully matching.I was thinking of writing a shell script using cut and grep and while loop but after going thru posts it... (2 Replies)
Discussion started by: blackjack101
2 Replies
Login or Register to Ask a Question