# Example for a similar case my @lines = ( "valid path /export/home/joe/data/01/", "ignore this one /var/apache/htdocs/", "valid path /usr/local/share/foobar/", "just testing /var/log/secure", "valid path /export/home/susan/recipes/" ); for (@lines){ if (m[^valid path\s+(/.*/)]){ print "$1\n"; } } #### /export/home/joe/data/01/ /usr/local/share/foobar/ /export/home/susan/recipes/