#!/usr/bin/perl use strict; use warnings; use IO::All -utf8; # Turn on utf8 for all io my $path = 'file.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { s{ ^\s* | \s*$ }{}gx for @smb; io($path)->appendln(@smb); }