The trick is taking into account the possibility of a circular reference in the source data, since netgroups may overlap and nest in many, many ways.

Pulling the data into a structure with a regex isn't as hard and deciding what kind of tree to use, and even more importantly, how you wish to represent the tree in a flat file format. Remember, you have a netgroup alias that maps to a collection of triples, which represent a host, a user, and a domain. Also, you may nest netgroups within netgroup definitions.

I don't know what data structure you want, but maybe this snippet will help with starting to parse the file.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $curr_grp; my (%host, %user, %domain); while (my $line = <DATA>) { if ($line =~ /^\s*(?!<\()([\w-]+)(?!=\))/) { $curr_grp = $1; } if ($curr_grp) { my @entries; push @entries, $1 while $line =~ /\((.*?)\)/g; for my $entry (@entries) { my ($host, $user, $domain) = split /,/, $entry; push @{$host{$curr_grp}} , $host if $host ; push @{$user{$curr_grp}} , $user if $user ; push @{$domain{$curr_grp}}, $domain if $domain; } } } print Dumper \%host; print Dumper \%user; print Dumper \%domain; __DATA__ nifty-group (host,,zanzibar.org) (host-1,,) (host-1.domain,,) (host_234,,) (foo,,) (host-1,phil,) (host-1.domain,,) (host_234,,) other-group (h2,,) (h6,,)

Hope this helped a little,
-v.

Update:Change code snippet to extract the data closer to what you want. Here's the output.

$VAR1 = { 'nifty-group' => [ 'host', 'host-1', 'host-1.domain', 'host_234', 'foo', 'host-1', 'host-1.domain', 'host_234' ], 'other-group' => [ 'h2', 'h6' ] }; $VAR1 = { 'nifty-group' => [ 'phil' ] }; $VAR1 = { 'nifty-group' => [ 'zanzibar.org' ] };

"Perl. There is no substitute."

In reply to Re: Process netgroup file with a regex by Velaki
in thread Process netgroup file with a regex by tweetiepooh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.