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( ' ', $line, ) )[2] // ''; # keep the fields uniq say $third unless $third eq $seen; $seen = $third; } close $fh;