Multiplication of two files and and sorting

Hi,
I have 2 ASCII files, say
file1

AAAAA	3.465830E-12
BBBBB	4.263280E-08
CCCCC	1.113320E-17
DDDDD	0.000000E+00
...

file2 with as many lines as file1

3.932350E-12 
1.194380E-07 
4.901480E-17 
0.000000E+00 
3.921180E-40 
0.000000E+00 
...

I would like to multiply the second column of file1 (C2F1) with the column of file2 (C1F2) into a file3 which then would contain

AAAAA	C2F1(1)*C1F2(1)
BBBBB	C2F1(2)*C1F2(2)
CCCCC	C2F1(3)*C1F2(3)
DDDDD	C2F1(4)*C1F2(4)
...

the lines of file3 should then be sorted from largest value of C2F1*C1F2 to the smallest.

Do you have any suggestion?

Many thanks,

file 1:                                               
AAAAA   3.465830E-12
BBBBB   4.263280E-08
CCCCC   1.113320E-17
DDDDD   0.000000E+00

file 2:

3.932350E-12
1.194380E-07
4.901480E-17
0.000000E+00

Script

awk 'FNR==NR{a[NR]=$1;next}{b[FNR]=$2;X[FNR]=$1;C=FNR;}
END{for(i=1;i<=C;i++)print X,a"*"b}' file2.txt file1.txt

output:

AAAAA 3.932350E-12*3.465830E-12
BBBBB 1.194380E-07*4.263280E-08
CCCCC 4.901480E-17*1.113320E-17
DDDDD 0.000000E+00*0.000000E+00

If it want to perform multiplication just remove double quotes at line 2 of below code:

awk 'FNR==NR{a[NR]=$1;next}{b[FNR]=$2;X[FNR]=$1;C=FNR;}
END{for(i=1;i<=C;i++)print X,a*b}' file2.txt file1.txt
1 Like

Try also

awk '{getline X < F1; print $1, X*$2}' F1=file2 file1 | sort -nr -k2 
CCCCC 5.45692e-34
BBBBB 5.09198e-15
AAAAA 1.36289e-23
DDDDD 0
1 Like

Hi.

Alternate methods:

#!/usr/bin/env bash

# @(#) s1	Demonstrate arithmetic combination of column data, awk, dm.
# For dm utility in |stat, see:
# http://hcibib.org/perlman/stat/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C paste awk dm

pl " Input data files data1 data2, combined into data3:"
paste data1 data2 > data3
head data1 data2 data3

pl " Results, awk:"
awk '{print $1,$2*$3}' data3

pl " Results, |stat dm:"
dm s1 'x2*x3' < data3

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
paste (GNU coreutils) 6.10
awk GNU Awk 3.1.5
dm - ( local: ~/executable/dm, 2009-11-09 )

-----
 Input data files data1 data2, combined into data3:
==> data1 <==
AAAAA	3.465830E-12
BBBBB	4.263280E-08
CCCCC	1.113320E-17
DDDDD	0.000000E+00

==> data2 <==
3.932350E-12 
1.194380E-07 
4.901480E-17 
0.000000E+00 

==> data3 <==
AAAAA	3.465830E-12	3.932350E-12 
BBBBB	4.263280E-08	1.194380E-07 
CCCCC	1.113320E-17	4.901480E-17 
DDDDD	0.000000E+00	0.000000E+00 

-----
 Results, awk:
AAAAA 1.36289e-23
BBBBB 5.09198e-15
CCCCC 5.45692e-34
DDDDD 0

-----
 Results, |stat dm:
AAAAA	1.36289e-23
BBBBB	5.09198e-15
CCCCC	5.45692e-34
DDDDD	0

Add the sort as desired.

Best wishes ... cheers, drl