Unstructured Scribbles

More
Akshay Patel

My name is Akshay Patel. I'm a Frontend Engineer in Los Angeles and I work at Yahoo! Sports.

Read more »

Perl

Battery Status

OSX Only

#!/usr/bin/perl

# battery.pl
#
# Perl script that displays to the terminal the charge on your Mac Intel battery (and takes
# a guess as to how much time is left on the charge).

use strict;
use Data::Dumper;
my $IOREG = "/usr/sbin/ioreg";

my $output = `$IOREG -nAppleSmartBattery`;
my @output = split("\n", $output);
my $batteryHash;

foreach (@output) {
    if (/\"(MaxCapacity)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(DesignCapacity)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(CurrentCapacity)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(AvgTimeToEmpty)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(AvgTimeToFill)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(TimeRemaining)\"\s+=\s+(\d+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(IsCharging)\"\s+=\s+(\w+)/) {
$batteryHash->{$1} = $2;
} elsif (/\"(FullyCharged)\"\s+=\s+(\w+)/) {
$batteryHash->{$1} = $2;
}
}

my $DEBUG = 0;
print Dumper $batteryHash if $DEBUG;

my $percentCapacity = ( $batteryHash->{"CurrentCapacity"} / $batteryHash->{"MaxCapacity"} ) * 100;
$percentCapacity = int($percentCapacity + .5 * ($percentCapacity <=> 0));

my $timeLeft;
if ($batteryHash->{"IsCharging"} eq "Yes" && $batteryHash->{"FullyCharged"} eq "No") {
    # battery is charging so can't calculate time left (yet)
    $timeLeft = "( + )";
    # $timeLeft = "(~/~)";
} elsif ($batteryHash->{"FullyCharged"} eq "Yes") {
    $timeLeft = "( f )";
} elsif ($percentCapacity != 100) {
    # OK, calculate time left
    $timeLeft = $batteryHash->{"TimeRemaining"} / 60;
    my ($hours, $minutes) = split(/\./, $timeLeft);
    $minutes = 60 * ( "." . $minutes );
    $minutes = int($minutes + .005 * ($minutes <=> 0));
    $minutes = sprintf ("%02d", $minutes);
    $timeLeft = "($hours:$minutes)";
} else {
    # not charging and 100% capacity
    # $timeLeft = "( - )";
    $timeLeft = "( f )";
}

print "Battery: $percentCapacity%\nTime Left : $timeLeft\n";

exit;
view raw battery.pl This Gist brought to you by GitHub.

Scrubbing two lists

Remove one list from another bigger list
Usage : perl filter.pl file1 file2>output
This will remove file2 elements from file1 and store it to output

#!/usr/local/bin/perl

$source=shift;
printhelp() if (! $source);

$remove=shift;

open(FILTER, "<$remove") || die "Can't open $remove\n";
open(SOURCE, "<$source") || die "Can't open $source\n";

$filter="";
while (<FILTER>)
{
$data=$_;
chomp($data);
$filter.=" $data ";
}
$notfilter=0;
$filtered=0;
while (<SOURCE>)
{
$data=$_;
chomp($data);
if ($filter=~/ $data /)
{
$filtered++;
}
else
{
$notfilter++;
print $data . "\n";
}
}

print "Removed: " . $filtered . "\n";
print "Left: " . $notfilter . "\n";


sub printhelp()
{
print <<END;
Usage:
perl filter.pl $list $removelist

END
}

view raw scrubber.pl This Gist brought to you by GitHub.

Unstructured Scribbles is powered by WordPress. Opinions on this website are my own and not of my employer.