Discussion:
Using AWK to multiply a column in a file with float number,
(too old to reply)
gpa...@gmail.com
2020-11-19 07:02:37 UTC
Permalink
Hi Experts,

I need to multiply a column in a file with float numbers, say with (1.1 + 0.25)

I have a extracted column 3 ( with Numbers) with awk command from file file1.txt

awk '{ print $3 }' file1.txt > coloumn1

1.0
1.2
1.3

I want to multiply all column values with (1.1 + 0.25)to achieve the following values. requirement is to use different p1 values. p1 is 1.1

( 1.0*(p1+0.25))
( 1.2*(p1+0.25))
( 1.3*(p1+0.25))

1.250
1.500
1.625

My script is

#bin/bash
p1=1.0
val=`echo $p1 + 0.25 | bc`
awk -F, '{$1*=$val}1' OFS=, coloumn1 > coloumn_mod

But above awk command is not returning the expected result , it gave like (1.1*$val)

Could you please help correcting the above command ?
Anssi Saari
2020-11-19 07:21:48 UTC
Permalink
Post by ***@gmail.com
My script is
#bin/bash
p1=1.0
val=`echo $p1 + 0.25 | bc`
awk -F, '{$1*=$val}1' OFS=, coloumn1 > coloumn_mod
But above awk command is not returning the expected result , it gave like (1.1*$val)
Could you please help correcting the above command ?
What about just using awk?

awk '{p1=1.1; print $1 * (p1+0.25)}' coloumn1 > coloumn_mod
Janis Papanagnou
2020-11-19 07:50:32 UTC
Permalink
Post by ***@gmail.com
Hi Experts,
I need to multiply a column in a file with float numbers, say with (1.1 + 0.25)
I have a extracted column 3 ( with Numbers) with awk command from file file1.txt
awk '{ print $3 }' file1.txt > coloumn1
1.0
1.2
1.3
I want to multiply all column values with (1.1 + 0.25)to achieve the
following values. requirement is to use different p1 values. p1 is 1.1
( 1.0*(p1+0.25))
( 1.2*(p1+0.25))
( 1.3*(p1+0.25))
1.250
1.500
1.625
My script is
#bin/bash
p1=1.0
val=`echo $p1 + 0.25 | bc`
If you want floating point shell arithmetic in shell I suggest to substitute
your first line by #!/usr/bin/ksh and use Kornshell's arithmetic expansion
$((...)) for your task. But as already noted elsethread all you need seems
to be to extend your awk program; awk is designed to work specifically well
on columnar data.
Post by ***@gmail.com
awk -F, '{$1*=$val}1' OFS=, coloumn1 > coloumn_mod
The problem is that awk does not know shell variables, you have to pass it,
e.g. as in

awk -v val="$val" '{ $1 *= val } 1'


Janis
Post by ***@gmail.com
But above awk command is not returning the expected result , it gave like (1.1*$val)
Could you please help correcting the above command ?
Kaz Kylheku
2020-11-20 04:17:27 UTC
Permalink
Post by ***@gmail.com
#bin/bash
Should be #!/bin/bash

If there is nothing Bash-specific, then #!/bin/sh.
Post by ***@gmail.com
p1=1.0
val=`echo $p1 + 0.25 | bc`
awk -F, '{$1*=$val}1' OFS=, coloumn1 > coloumn_mod
This is curious; why are you doing arithmetic with bc to prepare a
constant for use in an Awk script, when Awk has arithmetic (and the
script itself makes use of it by multiplying)?

Your $val is not interpolated because it's in single quotes.

You want something like:

awk -F, -v p1=$p1 'BEGIN { fac = p1 + 0.25 } { $1 *= fac; print }'

The -v argument creates an Awk variable on the command line;
we create an Awk variable called p1, whose value comes from the
text contained in the shell variable p1.

In the Awk code, when we refer to p1 that's the Awk variable; no
shell interpolation is going on.

In the BEGIN block we prepare the factor one time in the fac varaible,
and then refer to it.
--
TXR Programming Language: http://nongnu.org/txr
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1
Loading...