Multiply whole column with a value


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Multiply whole column with a value

Hi,
I need to multiply 3rd column (comma seperated) of entire file by a value say 2.2.

Suppose the file is:
Code:
C,Gas $ YTD(TRI),15512.36,01/01/2010

New file should be (3rd column value multiplied by 2.2):
Code:
C,Gas $ YTD(TRI),34127.192,01/01/2010


Last edited by Franklin52; 12-29-2010 at 09:04 AM.. Reason: Please use code tags
# 2  
Whichever suitable:
Code:
awk -F, '{$3=$3*2.2;print}' OFS=, inputFile

OR
Code:
awk -F, '{$3=$3*a;print}' a=2.2 OFS=, inputFile

OR
Code:
awk -F, '{$3=$3*a;print}' a=$var OFS=, inputFile

This User Gave Thanks to anurag.singh For This Post:
# 3  
Try
Code:
 awk 'BEGIN{FS=OFS=","}{print $1,$2,$3*2.2,$4 }' file.txt

This User Gave Thanks to posix For This Post:
# 4  
Code:
awk -F, '{$3=sprintf("%.2f",$3*2.2)}1' OFS=, infile

or if the precision does not need to be fixed:
Code:
awk -F, '{$3*=2.2}1' OFS=, infile


Last edited by Scrutinizer; 12-28-2010 at 12:45 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Quote:
Originally Posted by Scrutinizer
Code:
awk -F, '{$3=sprintf("%.2f",$3*2.2)}1' OFS=, infile

Should be:
Code:
awk -F, '{$3=sprintf("%.3f",$3*2.2)}1' OFS=, infile

# 6  
Quote:
Originally Posted by rdcwayx
Should be:
Code:
awk -F, '{$3=sprintf("%.3f",$3*2.2)}1' OFS=, infile

.2 is only an example. The OP can choose what value he likes...

Last edited by Scrutinizer; 12-28-2010 at 09:58 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add column and multiply its result to all elements of another column

Input file is as follows: 1 | 6 2 | 7 3 | 8 4 | 9 5 | 10 Output reuired (sum of the first column $1*$2) 1 | 6 | 90 2 | 7 | 105 3 | 8 | 120 4 |9 | 135 5 |10 | 150 Please enclose sample input, sample output, and code... (5 Replies)
Discussion started by: Sagar Singh
5 Replies

2. UNIX for Dummies Questions & Answers

Multiply value by row

Hi I would like to know how can I multiply the value of column three to columns 4-end of file example of input file: rs1000012 AK8 2 0.05 0.05 1 0 0 rs10000154 PAQR3 0.01 2 1 2 2 1 Desired output file: rs1000012 AK8 ... (1 Reply)
Discussion started by: fadista
1 Replies

3. Shell Programming and Scripting

Multiply certain column to variable

Hi experts, I want to multiply certain columns to variable, data : 1 2 3 4 5 6 7 8 9 result with var = 2 for column 3,6,9 ... (every columns which can be divided to 3): 1 2 6 4 5 12 7 8 18 I have tried : awk 'BEGIN{FS=OFS=" "}{print $1,$2,$3*a,$4,$5,$6*a,$7,$8,$9*2 }' a=2 file.txt but how... (6 Replies)
Discussion started by: guns
6 Replies

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

5. UNIX for Dummies Questions & Answers

Multiply two variables in t shell

Hello, How can I multiply two variables that contain floating point numbers. For example, how can I get the value for c a=2.165 b=3.234 c=a+3*b Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

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

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

8. Shell Programming and Scripting

Multiply variables

I have no idea about programming, just know some simple html :confused:and I need to get to somebody that can help me with creating the application (?) that will multiply 2 varibales and given price (height x lenght) x $$$. PLEASE HELP!:confused: edit by bakunin: Anita, as much as we... (2 Replies)
Discussion started by: Anita Flejter
2 Replies

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

10. Shell Programming and Scripting

gawk multiply one field

Hi, I am a newbie to gawk and I really don't know how to do this... I have a file with several columns of numbers and I want to make operations with some of the values... cat file.dat | gawk -v NAME=2 '/AS/ {TIME=$4} $1==NAME {print TIME,($3*1)}' The result of multiplying the field 3 by... (2 Replies)
Discussion started by: pau
2 Replies

Featured Tech Videos