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