I often use dpkg
or aptitude
combined with grep
when I want to list certain packages available or installed on my system, but I noticed that when I add | grep
, the output lines look a little bit different.
Here's a pure dpkg
output, the first command was typed when the terminal was smaller, the second one when the terminal was maximized:
As you can see, the output differs depending on the size of the window — the spaces are reduced in the case of a smaller one.
Now, what happens when we add | grep
:
A part of the first output was dropped to the second line. But when I maximised the terminal and typed the command once more, the line is in one piece. Moreover, the columns have the same fixed size (the same spaces between them).
This is an aptitude
output:
Both commands were typed in the maximised window, but the grep
line has narrower columns, and some text of the third column was cut off.
Why does it happen? Is there a way to stop grep
from resizing the lines?
I don't know how to add an image without changing its parameters, I hope you see what I'm talking about.
Best Answer
It is not
grep
changing the output. It isdpkg
andaptitude
. They check whether the output goes to a terminal or to some other command.If it is a terminal they adapt their own output width to match the terminal size.
If the output does not go to a terminal, the command has no idea what column size would be appropriate. (The output might as well end in some file.)
The same happens with
ls
. Comparels
andls|cat
.There is no general way to solve this, but some commands might have specific options for that. For example
aptitude
has--disable-columns
and-w
:The man page of
dpkg
says: