#!/usr/bin/env perl # # nr-h - rewrite numbers in the input to use suffixes K, M, G, T, P # # $Id$ # on the name: df | nr-h is similar to df -h, du | nr-h is similar to du -h use warnings; use strict; use Getopt::Std; my ($me) = $0 =~ m#([^/]*)$#; my %opt; getopts( 'h?x:', \%opt ) or HELP_MESSAGE(); ( $opt{h} || $opt{'?'} ) and HELP_MESSAGE(); my $x; !$opt{x} or eval { $x = qr/($opt{x})/ } or die "Invalid option value for -x: $opt{x} ($@\n).\n"; sub HELP_MESSAGE { print STDERR <) { print nrh_on_string($_); } } else { while (<>) { my @whatever = split /$x/; print join( '', map_odd( \&nrh_on_string, '', split /$x/ ) ); } } sub nrh_on_string { local ($_) = @_; join( '', map_odd( \&nrh, split /(\d+)/ ) ); } sub map_odd { my $sub = shift(@_); map { !( $_ % 2 ) ? $_[$_] : &$sub( $_[$_] ) } 0 .. $#_; } sub nrh { local ($_) = @_; s/(\d)\d{15}$/$1P/ or s/(\d)\d{12}$/$1T/ or s/(\d)\d{9}$/$1G/ or s/(\d)\d{6}$/$1M/ or s/(\d)\d{3}$/$1K/; $_; }