Multiply values from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Multiply values from a file
# 1  
Old 07-04-2019
Multiply values from a file

Hi All,

I extracted a file using an awk command like this

Code:
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 to do some math operation on the record in that file. I need do below operation
Code:
((COLUMN1*COLUMN2)/10000.0)*COLUMN3*(COLUMN4/1000.0)

I did the below operation to test the multiplication and I get some error. Can you help please
Code:
cut -d"," -f1*f2 OUTPUT
cut: invalid byte, character or field list
Try 'cut --help' for more information.

Thanks
Arun
# 2  
Old 07-04-2019
Why not use awk for the arithmetics as well?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-04-2019
Thanks the below worked . Is there a way I can sum those values to total. I get the each row doing the operation but I need to sum up those rows
Code:
awk '{print  (  ( substr($0,50,5) * substr($0,55,6) /100000 )*  substr($0,88,3)  * ( substr($0,81,7) /1000))          }

# 4  
Old 07-04-2019
Hi, try:
Code:
awk '
  {
    result=substr($0,50,5) * substr($0,55,6) * substr($0,88,3) * substr($0,81,7) / 10^8
    tot+=result
    print result
  } 
  END {
    print tot
  }
' file

In GNU awk, you can also do this:
Code:
awk -v FIELDWIDTHS="49 5 6 20 7 3 99"  '
  {
    result=$2 * $3 * $6 * $5 / 10^8 
    tot+=result
    print result
  } 
  END {
    print tot
  }
' file


Last edited by Scrutinizer; 07-04-2019 at 06:45 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

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): more file1.txt --> BLABLABLA BLABLABLA 1 2 3 4 1 2 3 1 2 1... (7 Replies)
Discussion started by: Youm
7 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. UNIX for Dummies Questions & Answers

Faster way to multiply a file Nth times?

Basically, my problem is to multiply my file to $c times. Is there a faster way to do this? c=100 while ]; do cat file1.txt ((c=$c-1)) done > file2.txt I appreciate your help! (6 Replies)
Discussion started by: chstr_14
6 Replies

7. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

8. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

9. Shell Programming and Scripting

All I want to do is Multiply...

All I want to do is find 5!. read num num={1..5} while do f= done echo f Error Msg. line 5: ${1..5} bad substitution line 6: integer expression expected Line 5 is the num=... Line 6 is the "while" statement I am new at this, and I am really, really trying. Please... (14 Replies)
Discussion started by: Ccccc
14 Replies

10. Shell Programming and Scripting

multiply variable

I need to multiply the value of a variable and then store it in another variable. I have EXPHOURINSEC=$(($EXPDATEHOUR * 3600)) but i get an error saying the * is unexpected. Im using ksh (4 Replies)
Discussion started by: fwabbly
4 Replies
Login or Register to Ask a Question