SebMa
SebMa

Reputation: 4709

awk arithmetic operation from string

I have a specific column in a file which contains strings like this :

1.1*1024
1.0*1024
1.1*1024
...

I want the numeric result :

1126.4
1024.0
1126.4

So I tried this, please note that the sizes are on the 6th column of my file :

$ cat file | awk '{col=$6;size=sprintf("%f", col);print "=> col = "col" size = "size}'
=> col = 1.1*1024 size = 1.100000
=> col = 1.0*1024 size = 1.000000
=> col = 1.1*1024 size = 1.100000

EDIT1 My input file looks like this :

$ cat file 
[[  ]]  toto1.mp4  2019-04-16  22:11  1.1*1024
[[  ]]  toto2.mp4  2019-04-16  21:41  1.0*1024
[[  ]]  toto3.mp4  2019-04-16  22:40  1.1*1024
[[  ]]  toto4.mp4  2019-04-16  22:09  1.1*1024
...

Somehow the multiplication is ignored.

Can you help ?

Upvotes: 0

Views: 779

Answers (6)

user7712945
user7712945

Reputation:

if data in d file, tried on gnu awk:

 awk -F'\\*' '{a=$1*$2; printf "%-6.1f\n",a}' d

Upvotes: 0

P....
P....

Reputation: 18351

This is generic way to to evaluate the string , it could process * ,+,-,/ etc. If you can use perl then there is option to eval the string, this will evaluate the string for the math operation from the input.

CASE-1: When evaluating entire line:

cat inputfile
1+1
2*2
3/3
4.5*2
5/2

perl -nle ' print eval ' inputfile
2
4
1
9
2.5

CASE-2: When evaluating one column only: In this case 6th column. supply the column number to get evaluated, F[5] is the 6th column.

perl -nale  'print  eval  $F[5]' inputfile
1126.4
1024
1126.4
1126.4

case-3: When evaluating 6th column, but printing entire record. Evaluate 6th column and update its value, later print the whole line.

perl -nale  '$F[5] = eval  $F[5] ;print "@F"' inputfile
[[ ]] toto1.mp4 2019-04-16 22:11 1126.4
[[ ]] toto2.mp4 2019-04-16 21:41 1024
[[ ]] toto3.mp4 2019-04-16 22:40 1126.4
[[ ]] toto4.mp4 2019-04-16 22:09 1126.4

Upvotes: 3

oguz ismail
oguz ismail

Reputation: 50750

sprintf can't evaluate operations itself. You need to parse and evaluate them, and then sprintf can be used for converting the result into a string with a specific format.

$ awk -v OFS='  ' 'split($NF,a,/\*/)>1{$NF=a[1]*a[2]} 1' file
[[  ]]  toto1.mp4  2019-04-16  22:11  1126.4
[[  ]]  toto2.mp4  2019-04-16  21:41  1024
[[  ]]  toto3.mp4  2019-04-16  22:40  1126.4
[[  ]]  toto4.mp4  2019-04-16  22:09  1126.4

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203189

Is this what you're trying to do?

$ awk -F'[[:space:]*]+' '{printf "%.1f\n", $(NF-1) * $NF}' file
1126.4
1024.0
1126.4
1126.4

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following(tested and written after seeing provided samples only).

awk '{split($NF,array,"*");printf("%.01f\n",array[1]*array[2])}' Input_file

Upvotes: 3

James Brown
James Brown

Reputation: 37394

Awk can't evaluate expressions in variables, you need to build an evaluator for it or evaluate some other way, like:

$ awk '{
    cmd="awk \47BEGIN{print " $6 "}\47"  # prepare an external awk command 
    cmd | getline retval                 # execute
    print retval                         # output evaluated value
    close(cmd)                           
}' file

Output:

1126.4
1024
1126.4
1126.4

There are awk programs floating around that can evaluate, for example calc3. Google is your friend, my friend.

Upvotes: 2

Related Questions