use strict; use warnings; use 5.010; sub extract_dbhosts { my @files = @_; my @dbhosts; my %seen; my $search = 'sapdbhost'; # keep it lower case # https://help.sap.com/saphelp_nw70ehp1/helpdata/en/c4/3a6136505211d189550000e829fbbd/frameset.htm FILE: for my $file ( @files ) { open my $fh, '<', $file or die "open($file,ro) failed: $!"; LINE: while ( my $line = <$fh> ) { # skip not matching lines next LINE if index( lc($line), $search, 0 ) < 0; # get value of assignment my ( $dbhost ) = (split m{\s*=\s*}, $line )[1]; # remove whitespaces (newlines) from dbhost $dbhost =~ s/\s+//g; # add db hosts in order of occurence push @dbhosts, $dbhost if not $seen{$dbhost}++; # assuming only one dbhost per file; otherwise comment out the "next FILE;" next FILE; } close $fh; } # return list of db hosts in order of occurence return @dbhosts; } ### use case: # get all profile files; SID is replaced with * my @profile_files = glob( "/usr/sap/*/SYS/profile/DEFAULT.PFL" ); # search through all profile files and get db_hosts my @db_hosts = extract_dbhosts( @profile_files );