Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare two sysctl outputs.
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 sysctl1.lst sysctl2.lst\n" if @ARGV != 2;
sub file2list($) {
my $fname = shift;
open F, $fname or die "Cannot open $fname: $!\n";
my @lines = <F>;
close F;
chomp @lines;
my %k = map { my ($a,$b) = split / = /, $_; $a => $b } @lines;
\%k
}
my $a = file2list($ARGV[0]);
my $b = file2list($ARGV[1]);
my (%onlya, %onlyb, %mismatch);
while (my ($k,$v) = each %$a) {
if (!exists($b->{$k})) {
$onlya{$k} = $v
} elsif ($b->{$k} ne $v) {
$mismatch{$k} = "$v == $b->{$k}"
}
}
while (my ($k,$v) = each %$b) {
$onlyb{$k} = $v if !exists($a->{$k});
}
sub print_list($$) {
my ($title, $listref) = @_;
print "\n==== $title ====\n";
printf("%-40s %s\n", $_, $listref->{$_}) foreach sort keys %$listref;
}
print_list "MISMATSH", \%mismatch;
print_list "ONLY AAA", \%onlya;
print_list "ONLY BBB", \%onlyb;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment