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

jl66
jl66 used Ask the Experts™
on
Have a file containning a line of data with two huge numbers as follows:
test.txt:
A:    123456789012   234567890123
Would like to get:
A:    117738     223701
where 117738 = 123456789012/1024/1024 and 223701= 234567890123/1024/1024
The module Math::BigInt may be needed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
sorta one line.

I put your one line in a file called "n"
I put the code in a file called n.pl

I ran "perl n.pl n"

and got
117738 223701

It appears from your example you wanted rounding.
$_ = <>; split; print int($_[0] / 1024 / 1024 + .5), " ", int($_[1] / 1024 / 1024 + .5), "\n";

Open in new window

perl -e '$_ = <>; split; print int($_[0] / 1024 / 1024 + .5), q( ), int($_[1] / 1024 / 1024 + .5), qq(\n);' < path_to_file

That's just Kevin_u's code, re-quoted to work as a one-liner proper, and using a redirect to get the file to the program, since <> will also accept STDIN, not just command line specified files.

I didn't vet Kevin's code for viability in general.  My sole contribution was in making this into a true one-liner as you'd asked for.
perl -lape '$_=join" ",map{int(.5+$_/1024/1024)}@F' input_file

or,

perl -pe 's,(\d+),int(.5+$1/1024/1024),ge' input_file

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Top Expert 2009
Commented:
Replace file1.txt with your file name.
perl -Mbignum -ane '$_=int($_/1024/1024+.5) for @F[1..2];print "@F\n"' file1.txt

Open in new window

Top Expert 2009

Commented:
The -Mbignum will make sure this works even if you have larger numbers.
Commented:

#!/usr/bin/perl
use strict;
 
open (IN,"test.txt") or die;
open (OUT,">test_out.txt") or die;
my @a=split (/\s+/,<IN>);
foreach (@a){
   $_=($_/1024**2);
   print int($_). "\n";
   print OUT int($_). "\n";
}

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