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
$swlist 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
$swlist program in a pipe to grab the output one line at a time, see
open.