Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Create based on repeating data

by Perl-Thornton (Initiate)
on Aug 24, 2016 at 00:36 UTC ( [id://1170268]=perlquestion: print w/replies, xml ) Need Help??

Perl-Thornton has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I have a file that looks like this:

Hostname1 1.1.1.1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 2.2.2.2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31
The repeating pattern seems obvious. Each row starts with either text that starts at the beginning of the row (i.e. hostname which could be the key to a hash), or the row starts with white space or tabs and then some text which could be the values associated with the key of the hash (i.e. serial number, interface, etc..). I have been working on solving this to parse through files with a Perl program but I've failed so far. I was hoping the Perl community might have suggestions to help me grasp what seems like should be a simple concept.

Replies are listed 'Best First'.
Re: Create based on repeating data
by BrowserUk (Patriarch) on Aug 24, 2016 at 00:57 UTC

    Like this?:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my( %data, %subhash, $last ); while( <DATA> ) { if( /^H/ ) { if( $last ) { $data{ $last } = { %subhash }; undef %subhash; } ( $last, my( $key, $val ) ) = m[(\S+)\s+(.+)\s+(\S+)$]; $subhash{ $key } = $val; } else { my( $key, $val ) = m[^\s+(.+)\s+(\S+)$]; $subhash{ $key } = $val; } } $data{ $last } = { %subhash }; pp \%data; __DATA__ Hostname1 1.1.1.1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 2.2.2.2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31

    Outputs:

    C:\test>1170268.pl { Hostname1 => { "1.1.1.1 " => "Cisco", "Chassis Serial Number" => "xyz123", Interface => "Gig0/0/31", }, Hostname2 => { "2.2.2.2 " => "Juniper", "Chassis Serial Number" => "abc123", Interface => "Gi-0/0/31", }, }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Avoiding the assumption that hostnames start with "H", and simplifying logic:
      #! perl -slw use strict; use Data::Dumper; my( @data, $current ); while( <DATA> ) { if( my ($host,$ip,$brand) =/^(\w+)\s+(\S+)\s+(\S+)/ ) { $current and push @data, $current; $current = {HOST=>$host, IP=>$ip, BRAND=>$brand}; next; } my( $key, $val ) = m[\s+(.+)\s+(\S+)$] or next; $current->{$key} = $val; } $current and push @data, $current; print Dumper \@data; __DATA__ Hostname1 1.1.1.1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 2.2.2.2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31
      Output:
      $VAR1 = [ { 'Chassis Serial Number' => 'xyz123', 'Interface' => 'Gig0/0/31', 'BRAND' => 'Cisco', 'HOST' => 'Hostname1', 'IP' => '1.1.1.1' }, { 'HOST' => 'Hostname2', 'BRAND' => 'Juniper', 'Chassis Serial Number' => 'abc123', 'Interface' => 'Gi-0/0/31', 'IP' => '2.2.2.2' } ];
      Alternative, using hostname as a key to a hash:
      my ($current,%data) ... $current and $data{$current->{HOST}} = $current; # in 2 places $current = {HOST=>$host, IP=>$ip, BRAND=>$brand};

              "Software interprets lawyers as damage, and routes around them" - Larry Wall

Re: Create based on repeating data
by GrandFather (Saint) on Aug 24, 2016 at 01:23 UTC

    Show us what you have tried. Show us the expected output. Tell us how it fails.

    The data you show is great in terms of size. Does it show any special cases you may need to deal with?

    Premature optimization is the root of all job security
Re: Create based on repeating data
by perldigious (Priest) on Aug 24, 2016 at 00:42 UTC

    Hi Perl-Thornton,

    I'm not quite understanding your question, perhaps you could add a little more to clarify for me and the rest of the monastery?

    You say you are trying to parse through the file, but what are you trying to extract exactly?

    I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
    I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1170268]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found