Skip to content

Instantly share code, notes, and snippets.

@kindy
Created January 17, 2014 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindy/8476631 to your computer and use it in GitHub Desktop.
Save kindy/8476631 to your computer and use it in GitHub Desktop.
used to count stream size. like tee, but not save stream to file.
#!/usr/bin/env perl
# head -c123456845 /dev/random | szstat >/dev/null
sub ps {
my $n=shift @_;
my @u=qw"B K M G T P E";
my $i=0;
while ($n>=1024 and $i<$#u) {
$n /= 1024.0;
$i += 1;
}
return sprintf("% 10.3f $u[$i]", $n);
}
$n = 2 * 1024; # copy per 2k
while ($n2 = read STDIN,$in,$n) {
print $in; # output everything
$c += $n2;
print STDERR ("\r" . ps($c)) if ($c%(1024)==0);
}
print STDERR "\ntotal: ", ps($c), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment