using awk or shell for multplication


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using awk or shell for multplication
# 1  
Old 01-08-2008
using awk or shell for multplication

Suppose if u have a file like that
file1
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002
etc..

another file
file2
1.2
3.2
-3.4
2.3
2.5
3.4
2.5
3.7
-4.5
2.4

So want to have each one in a file 1 multiply with another one in file 2 respectively
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002 ----->multiply by 1.2
0.01*0.02*0.04*0.06*0.03*0.05*0.04*0.009*0.002 ----->multiply by 3.2.
respectively......
That is want output to be like below
0.012*0.024*0.048*0.072*0.036*0.06*0.048*0.0108*0.0024
.....
# 2  
Old 01-08-2008
Code:
awk 'NR == FNR { 
    x[FNR] = $1
    next
    }
{ for (f=1; f<=NF; f++)
    $f *= x[FNR]
    }1' OFS='*' FS='*'  file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 01-08-2008
Quote:
Originally Posted by radoulov
Code:
awk 'NR == FNR { 
    x[FNR] = $1
    next
    }
{ for (f=1; f<=NF; f++)
    $f *= x[FNR]
    }1' OFS='*' FS='*'  file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
Thanks for ur fast reply
regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

2. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

3. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

4. Shell Programming and Scripting

Shell variable in awk

I have 3 files and each contain m*n matrix. m = number of rows (horizontal lines) n = number of columns (entries in a particular line) What I wish to find is the sum of the 2nd number in the last row. Ex file1.dat 2 5 8 8 4 6 7 8 3 8 3 7 file2.dat 3 4 1 4 8 4 0 3 4 7 3 7 ... (3 Replies)
Discussion started by: dynamics
3 Replies

5. Shell Programming and Scripting

shell command in AWK

Hi experts, Sorry if it sounds basic question. I am trying to delete all folders except a particular and I have written the following. But its not deleting the folder. Could you tell me whats wrong? folders are: 1 2 3 4 5 6 ls -l | awk '{ if ($9 != 4) {system(/bin/rmdir $9);print... (5 Replies)
Discussion started by: amitrajvarma
5 Replies

6. Shell Programming and Scripting

Shell Script using awk

File1 9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY 9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT 9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD 9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY File2 DPT.1072 CP~ Apr 1514283.914 (12 Replies)
Discussion started by: yale_work
12 Replies

7. Shell Programming and Scripting

shell / awk arrays

I have a bash shell script that sources a data file, the data file has array values such as: #--data file ---# sg_name="db1" sg_size="12892" sg_allow="50000" sg_name="db2" sg_size="12892" sg_allow="50000" export sg_name sg_size sg_allow #--end data file --# In my shell script... (8 Replies)
Discussion started by: lochraven
8 Replies

8. UNIX for Advanced & Expert Users

AWK instead of Shell script

I've a list file, which has some file names. ex: list file "list_file" will have data_file1.txt data_file2.txt data_file3.txt : : data_filen.txt Above files will have the below layout: Header1 Header2 *TM* Data record 1 Datarecord 2 Datarecord n *TM* (19 Replies)
Discussion started by: Lokesha
19 Replies

9. Shell Programming and Scripting

Using shell variables In awk

Oh its not my day for syntax... cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!! Every... (11 Replies)
Discussion started by: nortypig
11 Replies

10. UNIX for Advanced & Expert Users

shell / awk doubt

Hi all, I have a ascii text file like this : $cat a.txt abc |12343 |xyakdfj | x |323 || kr1 |13434343|234343 | Is it possible to to get the file eleminating blank spaces? output file : abc|12343|xyakdfj| x|323||... (7 Replies)
Discussion started by: krishna
7 Replies
Login or Register to Ask a Question