I could need some help with a oneline script im building. The script should give me a list of services, which are running on the current system. I wanted to realize this with systemctl.
The current oneliner looks like this:
systemctl list-units --type service | grep -f /etc/update-motd.d/service.list
However this command always creates a blank line between the services which are listed
smbd.service loaded active running Samba SMB Daemon
uuidd.service loaded active running Daemon for generating UUIDs
virtlogd.service loaded active running Virtual machine log manager
zabbix-agent.service loaded active running Zabbix Agent
I would like to remove the empty spaces. After some google research ive found some solutions with tr (like 'tr -d ""' or 'tr -s ""'), but this doesnt seem to work. Ive even tried some solutions with grep or sed but none of them helped me wih this.
edit: following solutions I tried without success: | grep "." | sed '/^$/d | grep "\S"
Some help would be great! Thanks
You can try something like this:
systemctl list-units --type service | grep -f /etc/update-motd.d/service.list | grep -v ^$
This should remove any emtpy linesUsing
tr
to squeeze newlines:What you are seeing are not empty lines - they're long trailing sequences of space characters that appear to be part of the
systemctl
tabular output:They appear as empty lines because of wrapping by the terminal.
You can remove them with
tr
, for example by squeezing horizontal whitespace:Another option, which preserves the tabular format better, would be to remove only trailing spaces:
There should be a way to stop
systemctl
from trying to format the output (or at least to be responsive to theCOLUMNS
environment variable), but I can't find it.