Is it possible to write a Perl one-liner to process a line data 2?

jl66 used Ask the Experts™
Have a file containning a line of data with two huge numbers as follows:
A:    123456789012   234567890123
Would like to get:
A:    117738     223701    52

where 117738 = 123456789012/1024/1024 and 223701= 234567890123/1024/1024 as well as
52 = 100*123456789012/234567890123

The module Math::BigInt may be needed.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

perl -lape '$_=join" ",(map{int(.5+$_/1024/1024)}@F),int(100*$F[0]/$F[1])' input_file

or, as adam314 mention, if you may have really big numbers:

perl -Mbignum -lape '$_=join" ",(map{int(.5+$_/1024/1024)}@F),int(100*$F[0]/$F[1])' input_file
Top Expert 2009
Did you realize that 52 is rounding a .6 fraction down, while the 117738 is rounding a .5 fraction up?  If this was intentional:
perl -Mbignum -ane '$_=int($_/1024/1024+.5) for @F[1..2];push @F,int(100*$F[1]/$F[2]);print"@F\n"' file1.txt

Open in new window



Thanks for the input.
Several windows systems do not take the ', but only ". However in your one-liner you have " within '. Could you please make proper changes?
Top Expert 2009
You need to use \" to have a double-quote inside the double-quote.  
#On windows:
perl -Mbignum -ane "$_=int($_/1024/1024+.5) for @F[1..2];push @F,int(100*$F[1]/$F[2]);print\"@F\n\"" file1.txt

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial