
mike3point0
Novice
Aug 17, 2013, 4:54 PM
Post #1 of 5
(3435 views)
|
Introducing a 2nd lookup to an already existing script
|
Can't Post
|
|
Got some assistance on a script I was working on a couple of months ago here, and wanted to ask another question. I've been trying to add another lookup to add to the mix. Here is the script: #!/usr/bin/perl use 5.10.0; use strict; use warnings; my $server_lst = 'new_unixlist'; my $non_prod = 'new_itmlist'; my $itm_found = 'InITMToday'; open my $srv_fh, '<', $server_lst or die "failed to open '$server_lst' $!"; open my $non_fh, '<', $non_prod or die "failed to open '$non_prod' $!"; open my $itm_fh, '>>', $itm_found or die "failed to open '$itm_found' $!"; my %server = map { chomp; $_ => 1 } <$srv_fh>; close $srv_fh; my $agentCounter = 0; while ( my $str = <$non_fh> ) { next if $str =~ /^#/; my $hostname = (split /:/, $str)[-2]; next unless $hostname; # sanity check to make sure the hostname field wasn't empty if ( exists $server{$hostname} ) { say {$itm_fh} "$hostname was found on line $. => $str"; $agentCounter++; } } say {$itm_fh} "Total number of servers with Agents is $agentCounter"; close $non_fh; close $itm_fh; I want to add another variable to the line where I write to the itm_fh filehandle and say "$hostname $itsos was found on line $. => $str" Noting that $itsos is a new field, taken from a file with the values: server1 HP-UX server2 AIX There are 2 values on each line in the new text file, separated by a space/tab. I'm thinking I have to read these values into an hash, something to the effect of: my %serveroses = <$os_fh>; close $os_fh; while (($samehost, $itsos) = each %serveroses) { say {$itm_fh} "$hostname was found on line $. => $str running $itsos"; } Place this right after the statement that reads "if ( exists $server{$hostname} ) {" Any thoughts or suggestions are appreciated.. Thanx. Mike-
|