Use a subroutine for the common code, passing in the variable parts are parameters. However, these lines are not that similar. The first one does an echo and the others call
as a program.
To make the code more effective in perl you can easily replace the grep program by a perl regular expression (make sure you are using Extended RE constructs in the regular expressions - grep uses Basic REs which differ slightly). You can run the
.