Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

regex for rpm: name, version, release, arch

by mhearse (Hermit)
on Aug 30, 2012 at 17:37 UTC ( #990806=perlquestion: print w/ replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of rpms, which I'm trying to break up into name, version, release and arch
rpm -qa fipscheck-1.2.0-1.el5.x86_64 iptables-ipv6-1.3.5-5.3.el5_4.1.x86_64 libXi-1.0.1-4.el5_4.i386 perl-Carp-Clan-5.3-1.2.1.noarch rpm-build-4.6.1-2.el5.x86_64
Can some one help me fix my broken regex?
my ($name, $version, $release, $arch); if ($line =~ /^(\w-+)-(\d.+)-(\d?)\.(\w+)$/) { $name = $1; $version = $2 $release = $3 $arch = $4; }

Comment on regex for rpm: name, version, release, arch
Select or Download Code
Re: regex for rpm: name, version, release, arch
by choroba (Abbot) on Aug 30, 2012 at 17:55 UTC
    I am not sure myself sometimes where version becomes release. What about this one?:
    #!/usr/bin/perl use warnings; use strict; use Text::Table; my $tb = Text::Table->new(qw/NAME VERSION RELEASE ARCH/); while(<DATA>) { $tb->load( [ /^(.*)-(.*)-(.*)\.(.*)$/ ] ); } print $tb; __DATA__ fipscheck-1.2.0-1.el5.x86_64 iptables-ipv6-1.3.5-5.3.el5_4.1.x86_64 libXi-1.0.1-4.el5_4.i386 perl-Carp-Clan-5.3-1.2.1.noarch rpm-build-4.6.1-2.el5.x86_64
    Output:
    NAME VERSION RELEASE ARCH fipscheck 1.2.0 1.el5 x86_64 iptables-ipv6 1.3.5 5.3.el5_4.1 x86_64 libXi 1.0.1 4.el5_4 i386 perl-Carp-Clan 5.3 1.2.1 noarch rpm-build 4.6.1 2.el5 x86_64
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Can you not use rpm queryformat option to do all the work for you?
      rpm -qa --qf "%{n},%{v},%{r},%{arch}\n"
Re: regex for rpm: name, version, release, arch
by philiprbrenan (Monk) on Aug 30, 2012 at 18:06 UTC

    I doubt my solution is generic enough for you, although it matches the data supplied and so might form a starting point. sub table() is included to make the output a bit more readable.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; sub table($;$$) # Table formatter {my ($d, $c, $w) = @_; # Data, control, width my @D; for my $e(@$d) {for my $D(0..$#$e) {my $a = $D[$D] // $w->[$D] // 0; my $b = length($e->[$D]); $D[$D] = ($a > $b ? $a : $b); } } my @t; for my $e(@$d) {my $t = ''; for my $D(0..$#$e) {if (substr(($c//'').('L'x($D+1)), $D, 1) =~ /L/i) {$t .= substr($e->[$D].(' 'x$D[$D]), 0, $D[$D])." "; } else {$t .= substr((' 'x$D[$D]).$e->[$D], -$D[$D])." "; } } push @t, $t; } @t } my @t = ([qw(name version release arch)]); for(split /\n/, << 'END') # Parse sample data fipscheck-1.2.0-1.el5.x86_64 iptables-ipv6-1.3.5-5.3.el5_4.1.x86_64 libXi-1.0.1-4.el5_4.i386 perl-Carp-Clan-5.3-1.2.1.noarch rpm-build-4.6.1-2.el5.x86_64 END {my ($name) = (split /\./)[0] =~ s/..\Z//r; my ($version) = /-([\d\.]+)/; my ($release) = /.*-(.*)\./; my $arch = (split /\./)[-1]; push @t, [$name, $version, $release, $arch]; } say for table([@t]); # Format data

    Produces

    name            version  release      arch    
    fipscheck       1.2.0    1.el5        x86_64  
    iptables-ipv6   1.3.5    5.3.el5_4.1  x86_64  
    libXi           1.0.1    4.el5_4      i386    
    perl-Carp-Clan  5.3      1.2.1        noarch  
    rpm-build       4.6.1    2.el5        x86_64 
    
      I apologize. This question has been addressed by a previous post.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990806]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-12-28 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls