#!/usr/bin/perl use strict; use warnings; use File::Slurp; my $filePath = 'test_1.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { map { s/\s+\z// } @smb; # remover white space append_file( $filePath, "chkconfig smb: OK\n"); append_file( $filePath, map { "$_\n" } @smb); # append_file( $filePath, "\n"); # if you want to append multiple times }