use strict; use warnings; use 5.010; my $file = "/usr/sap/SID/SYS/profile/DEFAULT.PFL"; my $search = 'dbhost'; open my $fh, '<', $file or die "open($file,ro) failed: $!"; my %seen; LINE: while ( my $line = <$fh> ) { # skip lines without search string next LINE if index( lc($line), lc($search), 0 ) < 0; # extract the third field of line my $third = ( split( m/\s+/, $line, ) )[2]; # keep the fields uniq $seen{$third}++; } close $fh; # show the third fields of matching lines, but only once say join( "\n", keys %seen );