awk multiply values contained in 2 different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk multiply values contained in 2 different files
# 1  
Old 02-22-2013
awk multiply values contained in 2 different files

Hi Everyone !

I have two files with the same configuration
and I want to multiply corresponding values and write the result in a file.
Let say 2 header lines and then lines of values (with not constant number of columns):
Code:
more file1.txt --> 
BLABLABLA 
BLABLABLA 
1 2 3 4 
1 2 3 
1 2 
1 2 3

Code:
more file2.txt --> 
BLABLABLA 
BLABLABLA 
2 2 2 2
2 2 2 
1 1 
1 1 1

I want to have this result:
Code:
more file3.txt --> 
BLABLABLA 
BLABLABLA 
2 4 6 8 
2 4 6 
1 2 
1 2 3

Anyone has an idea ? Thanks in advance,
Y.
# 2  
Old 02-22-2013
Code:
paste file1.txt file2.txt | awk '/^[0-9]/ {
                for(i=1;i<=NF/2;i++) {
                        s=s OFS $i*$(NF/2+1);
                }
                sub(/^ /,x,s);
                $0=s;
                s=""
} !/[0-9]/{
                $0=$1;
}1'

Note: Use nawk instead in Solaris or SunOS
# 3  
Old 02-22-2013
Thanks a lot, I am very impress, really!
It works very well for my example but indeed I have forgot to say that
-each line start with 1 or more whitespace
-first value can be negative so first char can be "-"
# 4  
Old 02-22-2013
The code gives you an idea on how to perform the arithmetic. Now why don't you modify the regexp as per you input file? Give it a try and let us know if you are stuck.
# 5  
Old 02-22-2013
Of course, I am on it. Thanks again.
# 6  
Old 02-22-2013
Quote:
Originally Posted by bipinajith
Code:
paste file1.txt file2.txt | awk '/^[0-9]/ {
                for(i=1;i<=NF/2;i++) {
                        s=s OFS $i*$(NF/2+1);
                }
                sub(/^ /,x,s);
                $0=s;
                s=""
} !/[0-9]/{
                $0=$1;
}1'

Note: Use nawk instead in Solaris or SunOS
Hi bipinajith,
Nice code, (and it works for the given sample) but I think there is a typo. Did you intend to use:
Code:
                        s=s OFS $i*$(NF/2+i);

instead of
Code:
                        s=s OFS $i*$(NF/2+1);

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-22-2013
Yes Don, that was my bad. Thanks.
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply values from a file

Hi All, I extracted a file using an awk command like this awk '{print substr($0,78,5)"," substr($0,59,6) "," substr($0,81,3) "," substr($0,11,7)}' file1 >> OUTPUT cat OUTPUT 00001,100000,005,0000080 00001,100000,008,0000220 00001,100000,001,0001000 00010,100000,001,0000400 I want... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Compare two files column values using awk

Judi # cat File1 judi /export/home 76 judi /usr 83 judi # judi # cat File2 judi /export/home 79 judi /usr 82 judi # if COLUMN3 of File2 is greater that COLUMN3 of File1, then print File2's lines juid /export/home 79 Code tags please (2 Replies)
Discussion started by: judi
2 Replies

3. Shell Programming and Scripting

How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size. 19BC 2363 20G 1AA3 2363 2.93T 1A94 2363 750G Whenever I come across a G I want to delete the G and multiply by... (2 Replies)
Discussion started by: kieranfoley
2 Replies

4. Shell Programming and Scripting

Creating a Third File from Information Contained in Two Files

In advance, I appreciate any help or tips. I apologize for not providing examples of scripts I have already tried, the reason is that I am new to programming and do not really no where to start. I think this is possible with awk, but do not know how to go about learning how to write the script... (2 Replies)
Discussion started by: awc228
2 Replies

5. UNIX for Dummies Questions & Answers

Searching for terms contained in 2 separate files

Hi All ! As a dummy, I try to search for multiple string of characters contained in 2 types of list. Not clear? No it's not. Example: I have a tab-delimited file called "inventory" with 2 columns that looks like that: #ref #year,color xrt3 2000,blue gf5 2000,red,green,yellow... (4 Replies)
Discussion started by: lucasvs
4 Replies

6. Shell Programming and Scripting

multiply with awk

HI help i have cc 9+37.50 328611.50 688498.25 42.38 cc 66+62.50 328636.50 688498.42 42.58 i want to make o/p cc 9+3750 328611.50 688498.25 42.38 cc 66+6250 328636.50 688498.42 42.58 plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

7. Shell Programming and Scripting

Help fixing awk code to print values from 2 files

Hi everyone, Please help on this: I have file1: <file title="Title 1 and 2"> <report> <title>Title 1</title> <number>No. 1234</number> <address>Address 1</address> <date>October 07, 2009</date> <description>Some text</description> </report> ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

8. Shell Programming and Scripting

Multiply numbers from different files

Hi All, I have tried few things with this but it did not help much. I have some 200,000 files in a directory. There are two sets of files. 1. Files with extension .dat with file names like these (1.dat, 2.dat, 5.dat, 8.dat....200000.dat) 2. Another set of files with .txt extension and... (5 Replies)
Discussion started by: shoaibjameel123
5 Replies

9. Shell Programming and Scripting

awk multiply fields within lines, then total

Here is my scenario, I have two columns. I need to add the first columns (the easy part). The second column is a percent that I need to subtract by 100 then multiply against the first column for each line, then sum the output for a total Here is the input: 942330863 96 942172150 95... (4 Replies)
Discussion started by: mminaz
4 Replies

10. Programming

random array index returning values not contained

For kicks I wrote up a Password generator after lunch. Let me start with the code: unsigned int x,y,z,c; unsigned int KISS(); unsigned int devrand(); int main( int argc, char** argv ) { int i, j = 1; char pwd = "abcdefghijklmnopqrstuvwxyz" ... (5 Replies)
Discussion started by: VRoemer
5 Replies
Login or Register to Ask a Question