Multiplication of two files and and sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiplication of two files and and sorting
# 1  
Old 08-07-2014
Multiplication of two files and and sorting

Hi,
I have 2 ASCII files, say
file1

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

file2 with as many lines as file1

Code:
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

Code:
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,
# 2  
Old 08-07-2014
Code:
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
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[i],a[i]"*"b[i]}' file2.txt file1.txt

output:
Code:
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:
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[i],a[i]*b[i]}' file2.txt file1.txt

This User Gave Thanks to bharat1211 For This Post:
# 3  
Old 08-07-2014
Try also
Code:
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


Last edited by RudiC; 08-07-2014 at 09:40 AM.. Reason: forgot the sort...
This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-07-2014
Hi.

Alternate methods:
Code:
#!/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:
Code:
$ ./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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

3. Shell Programming and Scripting

Selective multiplication of two columns in two files

Hi again, I have two files e.g. file2 e.g. file1 BB152 6.279650E+02 AA124 6.279650E+02 AA124 6.0273E-01 9.7800E-01 AA124 6.3239E-01 9.7800E-04 AA124 6.4585E-01 7.3839E-02 BB152 6.6250E-01 2.4450E-04 BB152 7.0932E-01 1.3496E-02 CC124 7.1378E-01 2.2690E-02 CC124 7.2279E-01... (8 Replies)
Discussion started by: f_o_555
8 Replies

4. Shell Programming and Scripting

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 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... (4 Replies)
Discussion started by: f_o_555
4 Replies

5. Shell Programming and Scripting

Sorting files

I have the following set of files and I want to order them according to the ascending values of the run: For example, doing ls -lrt *drw*.log gives n02-z30-sr65-rgdt0p25-dc0p08-4x3drw.log n02-z30-sr65-rgdt0p25-dc0p03-8x6drw.log n02-z30-sr65-rgdt0p25-dc0p01-8x6drw.log ... (18 Replies)
Discussion started by: kristinu
18 Replies

6. Shell Programming and Scripting

i need help in sorting two files

i have file a 123 234 456 567 678 and file b 123|xxx|hhh|ppp or zzz 234|rrr|ttt|xxx 432|ttt|mmm|nnn 678|cft|byt|mop i want to compare file a to file b such that when each of the lines in file a can be found in file b column1 and also xxx or hhh or ppp or zzz can be... (12 Replies)
Discussion started by: blackzinga80
12 Replies

7. UNIX for Dummies Questions & Answers

Matrix multiplication with different files

Hi, i have file1 which looks like: x1 y1 z1 x2 y2 z2 ...(and so on) and file2 which looks like: a11 a12 a13 a21 a22 a23 a31 a32 a33 and i want to replace file1 with the following values: x1' y1' z1' x2' y2' z2' ...(and so on) (2 Replies)
Discussion started by: ezitoc
2 Replies

8. Shell Programming and Scripting

sorting files

hi i have file like below: col1,col2,col3,col4 val1,val2,val3,val4 abc1,abc2,abc3,abc4 this is a 4 column file with 3 rows. i want to sort the file like.. first on col1, then on col2 and so ..on.. i want the sort order to be descending. Pls help.. Thnks Sumit (2 Replies)
Discussion started by: sumit207
2 Replies

9. Shell Programming and Scripting

Sorting Files

How to sort such files which contains records of varying length and varying lines? (With respect to Bash shell) Eg: Each record begins with a sting of 1/0(binary) which may or may not be followed by properties like AB,BS etc. I have to sort such records on the basis of 1/0 string and keep the... (2 Replies)
Discussion started by: sandeep_hi
2 Replies

10. UNIX for Advanced & Expert Users

sorting files

ok so I'm having major issues trying to figure this out: I have this program that I'm inputting the files in hte current directory which are image files...it spits out 5 line chunks describing the files... filename: (name of file) size: (100 x 200) arbitrary data arbitrary data arbitrary... (4 Replies)
Discussion started by: Infraredskies
4 Replies
Login or Register to Ask a Question