Skip to main content

grep breaks formatting of systemctl [Resolved]

If I call command systemctl without grep, then formatting looks correctly:

enter image description here

Now I want to filter only lines with 'yarn', but this time line is broken into two parts:

enter image description here

same happens when console is two times wider. How can i fix this?


Question Credit: stiv
Question Reference
Asked October 10, 2018
Posted Under: Unix Linux
40 views
1 Answers

When systemctl’s output isn’t sent to a terminal, e.g. if it’s piped to grep, then it allows each column of its output to grow as much as necessary to fit the longest item in the list. This is what’s happening here: some unit in the list of units has a very long name, and aligning the output of all units produces the large space between yarn.service and loaded failed failed.

To filter the unit output, use systemctl’s built-in pattern matching:

sudo systemctl list-units '*yarn*'

You can limit the output to the lines you’d get with grep with the --no-legend option, avoid colouring and special characters with the --plain option, and avoid starting a pager with the --no-pager option:

sudo systemctl list-units --plain --no-legend --no-pager '*yarn*'

There are better sub-commands if you want to retrieve the status of specific units; see for example The "proper" way to test if a service is running in a script.


credit: Stephen Kitt
Answered October 10, 2018
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA