Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
...But I'm looking for the host_name and address in addition to the line...
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %host_add; my ( $host, $name ); while (<DATA>) { s/^\s+|\s+$//; if (/^host_name/) { ( $host, $name ) = split /\s+/, $_; if ( !exists $host_add{$name} ) { undef $host_add{$name}; } else { next } } elsif ( /^address/ or /^hostgroups/ ) { my ( $add, $address ) = split /\s+/, $_; push @{ $host_add{$name} }, $address; } } print Dumper \%host_add; __DATA__ define host{ use HALF host_name denlas02 alias denlas02 address 146.xxx.xxx.xxx hostgroups LINUX,DEN,DMZ } define host{ use HALF host_name ppplas11 alias ppplas11 address 10.xxx.xxx.xxx hostgroups LINUX,PPO,ORAPRD } define host{ use ALIVE host_name ppplas12 alias ppplas12 address 10.50.33.26 hostgroups LINUX,GRID,RMAN,CRIT }
output
$VAR1 = { 'denlas02' => [ '146.xxx.xxx.xxx', 'LINUX,DEN,DMZ' ], 'ppplas11' => [ '10.xxx.xxx.xxx', 'LINUX,PPO,ORAPRD' ], 'ppplas12' => [ '10.50.33.26', 'LINUX,GRID,RMAN,CRIT' ] };
With the above as output, you can get the host, hostgroup, and address..
Add the following to the script like so:
for my $val ( keys %host_add ) { for ( @{ $host_add{$val} } ) { print $val, q{ }, join " ", @{ $host_add{$val} }, $/ if $_ =~ m{CRIT|DMZ}; } }
Then, your output will be:
denlas02 146.xxx.xxx.xxx LINUX,DEN,DMZ ppplas12 10.50.33.26 LINUX,GRID,RMAN,CRIT
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: split file on blank lines and match blocks by 2teez
in thread split file on blank lines and match blocks by raggmopp

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found