DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Parse Solaris Pkginfo -l In Perl

11.02.2009
| 2954 views |
  • submit to reddit
        // description of your code here

#!/usr/bin/perl -w
#
#
#
use strict;
use Data::Dumper;

my $pkg_list = "/usr/bin/pkginfo -l|";

open (PKG_LIST, $pkg_list) || die "Can't run '$pkg_list': $!\n";
my $pkg;
while (defined (my $line = <PKG_LIST>)) {

    chomp $line;

    my $match = ( $line =~ /PKGINST:/ .. $line =~ /^$/ );

    if ( $match && $match !~ /E0/ ) {

        if ( $line =~ /^\s+([A-Z]+):\s+(.*)$/ ) {
            my ($key, $val) = ($1, $2);
            if ( $key eq 'FILES' ) {
                if ( $val =~ /^(\d+) (.*)$/ ) {
                    $pkg->{FILES}->{$2} = $1;
                }
            }
            else {
                $pkg->{$1} = $2;
            }
        }
        elsif ( $line =~ /^\s+([0-9]+) (.*)$/ ) {
            $pkg->{FILES}->{$2} = $1;
        }
        else {
            print "Unrecognized output: [$line]\n";
        }

    }
    else {

        #
        # Blank line between packages
        #
        print Dumper $pkg;

    }
}
close PKG_LIST;