Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: A little golf anyone?

by petral (Curate)
on Oct 31, 2002 at 00:00 UTC ( #209285=note: print w/replies, xml ) Need Help??

in reply to A little golf anyone?

Kinda silly at 143:
perl -pe's/(^|\s)((\d?\d)?(\d)(\d)\d{5}|(\d?\d)?(\d)(\d)\d\d|\d{1,3})( +?=\s|$)/$1.($3?" $3$4G":$4?" $4.$5G":$6?" $6$7M":$7?" $7.$8M":$2) +/ge'
but it does produce:
/dev/dsk/c0t1d0s1 1.2G 814M 391M 68% /usr /dev/dsk/c0t1d0s3 245M 44M 176M 21% /var /dev/dsk/c0t1d0s0 424M 187M 194M 49% /home
from your sample (note truncation instead of rounding).
Maybe I can think of something more sensible in the morning.

upday:   Ok, this is simpler (102 chars):
perl -pe's/(^|\s)((\d*?)\d)(\d)\d\d(\d{3})?(?=\s)/$1.($5&&" ").($3?" + $2":"$2.$4").($5?"G":"M")/sge'
It still preserves numeric alignment with the original text, eg,
$ echo \ 1234 12 12345 123456 1234567 12345678 123456789 | perl -pe'. . .' 1.2M 12 12M 123M 1.2G 12G 123G
and rounding adds 22 (to 124 chars):
perl -pe's/(^|\s)((\d*?)\d)(\d)(\d)\d(\d{3})?(?=\s)/$1.($6&&" ").($3 +?" ".($2+($4>4)):$2+($4+($5>4))/10).($6?"G":"M")/ges'
Update, the second:

Here's a funny format one at 79 chars:
perl -pe's/((\d*?)\d)(\d)\d\d(\d{3})? /$1.($2?" ":".$3").($4?" G ": +"M ")/ge'
which produces this:
$ echo ' 1234 12 123 12345 123456 7654321 12345678 123456789 ' | perl -pe'... 1.2M 12 123 12 M 123 M 7.6 G 12 G 123 G
And an extendible one at 114 chars (got to be ready for those terrabyte-sized disks):
perl -pe's!(^|\s)((\d*?)\d)(\d)\d\d((\d{3})*)(?=\s)!$"x($x=length$5).$ +1.($3?" $2":"$2.$4").qw(K M G T)[$x/3]!ges'
Which does:
$ echo \ 1234 1234567 123456789012 1234567890123 123456789012345 | perl ... 1.2K 1.2M 123G 1.2T 123T
Ok, here's a real one at 117 chars that does 1024-type m's and g's and t's:
perl -pe's!\d{4,}(?=\s)!sprintf" "x($x=(-4+length$&)/3).($x*3%3?" % +.0f":"%.1f").qw(M G T)[$x],$&/1024**int$x+1!ge'
Which gets:
$ echo ' 321 4321 654321 87654321 9876543210 98765432101 987654321012 '|perl... 321 4.2M 639M 84G 9.2T 92T 920T
This ignores several problems:  1) Before 5.6, perl expanded   `qw(...)' into   `split" ",'...'' (so you couldn't   [index]it );  2) There's no protection against filenames or whatever which end with a string of 4 digits;  and 3) There's no provision for catching a number at the end of the line.

Adding these back in, yields 132 chars:
perl -pe's!(^|\s)(\d{4,})(?=\s)!sprintf"$1%s".(($x=-4+length$2)%3?" % +.0f":"%.1f").(qw(M G T))[$x/=3]," "x$x,$2/1024**int$x+1!ges'

(BTW, what comes after Terrabytes?)

Update the last:   Updated the above to save a few strokes (and round properly on the 1024 one (changed %d -> %.0f)).


Replies are listed 'Best First'.
Re: Re: A little golf anyone?
by duelafn (Vicar) on Oct 31, 2002 at 04:06 UTC
    Shorter sillyness (read cheating). 54 characters. df -k|perl -pe's/\d{3} / M  /g;s/(\d+)(\d)\d\d M/ $1.$2 G/g'

    If we didn't reinvent the wheel, we wouldn't have rollerblades.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://209285]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-06-02 09:01 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (16 votes). Check out past polls.