If you run sar
without parameters, the output is in color, but with extra headers inserted after each page. If you run sar | cat
the extra headers are removed, but so is the coloring.
If you would like to pipe into less, running sar | less
have the same effect as using cat
- extra headers are removed, but so are colors. If you would like to preserve color with less
, you can use unbuffer
(which is installed as a part of expect
) and less -R
, but then the extra headers appear again.
I believe this is better shown in pictures below (the extra headers are marked in image 2).
The result of unbuffer sar | less -R
:
So is it at all possible to preserve colors with the sar
command using less
, and at the same time not have the extra header lines for each page (as marked in image 2)?
I reckon it might be possible to do this with some elaborate awk
/grep
/sed
piping (and maybe chained commands), but I would prefer a more straight solution with the commands themselves, if this is at all possible. This is also due to the fact that I would like to pipe the result to less
using a single command.
I'm using Ubuntu Server 22.04.
I have never used
sar
, butman sar
mentions thatsar
has an environment variable whose value controls this behavior:There are various tools that change their output depending on whether they are being piped to something or just printing toa terminal and many have an
always
option to ensure color is used regardless. For instance, see the--color
options ofgrep
andls
. It looks likesar
is using an environment variable for the same thing, so just try this:The
-R
option forless
allows the preservation of colors and hyperlinks in the text being piped.