14

When I SSH to a machine with ssh machine and run yum list it outputs everything that's installed as expected, and importantly every item is on one line like so:

xz-devel.i386                                                                                                        4.999.9-0.3.beta.20091007git.el5                                                                  base            
xz-devel.x86_64                                                                                                      4.999.9-0.3.beta.20091007git.el5                                                                  base            
xz-libs.i386                                                                                                         4.999.9-0.3.beta.20091007git.el5                                                                  base            
xz-lzma-compat.x86_64                                                                                                4.999.9-0.3.beta.20091007git.el5                                                                  base            

But upon doing it remotely with ssh machine 'yum list' or even piping it to grep while on the server with yum list | grep xz -C 3 the output's lines are "truncated" and made much shorter, like so:

xz-devel.i386                              4.999.9-0.3.beta.20091007git.el5
                                                                       base     
xz-devel.x86_64                            4.999.9-0.3.beta.20091007git.el5
                                                                       base     
xz-libs.i386                               4.999.9-0.3.beta.20091007git.el5
                                                                       base     
xz-lzma-compat.x86_64                      4.999.9-0.3.beta.20091007git.el5
                                                                       base     

How can I make the size of the line not shrink like this? My end desire is to pipe this to tee and then do some processing on the outputted file for all my hosts via ansible.

It seems that this is occurring due to some sort of "virtual terminal" as part of the SSH/piping because I can get the same behavior if I shrink my terminal size where the lines break when running yum list. Thus I'd assume that the addition of the pipe somehow tells yum list that the width of the terminal is only X columns

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Mitch
  • 1,198
  • 1
    http://stackoverflow.com/questions/18964478/rearranging-file-into-three-columns/18964631#18964631 – thrig Apr 07 '16 at 15:55
  • cannot reproduce on centos 7 – sivann Apr 07 '16 at 16:21
  • 1
    Note also that yum list does not list everything that is installed (it lists everything that is available). For a list of installed packages, you probably want rpm -qa, the output of which is much more suitable for scripting parsing. In particular, also see the documentation for the --queryformat option to the rpm command. – larsks Apr 07 '16 at 16:52
  • Does ssh -t machine 'yum list' solve the problem? – egmont Apr 07 '16 at 18:55
  • @thrig thanks for the link, that resolved it for me – Mitch Apr 08 '16 at 18:50
  • 1
    idk if this would solve in your specific situation, but often piping to cat is enough to tell a program that stdout isn't a tty, and therefore don't prettify output. w/ yum, for example, doing that will prevent right-justification of the trailing columns. – RubyTuesdayDONO May 17 '18 at 20:00

5 Answers5

9

Per this answer provided in the comments by @thrig I was able to get it to output correctly by doing

yum list installed | xargs -n3 | column -t 
rogerdpack
  • 1,715
Mitch
  • 1,198
  • I'd be curious to hear if my proposal (in a comment above) works for you; that'd be way simpler and catch the real cause why combining ssh and yum in a single step gives a different result than interactively logging in and executing yum remotely. I don't have a remote machine to test my theory. – egmont Apr 08 '16 at 18:56
  • 2
    Slightly modified: yum list installed | tail -n +5 | xargs -n3 | column -t | less cleans off the crud for unregistered redhat systems. – EdwinW Dec 11 '18 at 02:13
  • @egmont I tried your idea, and it worked quite well. – EdwinW Dec 11 '18 at 02:18
4

Install expect, which comes with unbuffer(1), and run:

# unbuffer yum repolist
3

I fought with this today (during a yum check-update operation) and after finding a "CLOSED NOTABUG" bugzilla report and various python hacks, I came up with yet another workaround:

script -q -c "stty cols 150; yum check-update" /dev/null > /path/to/output.txt 2>&1

or:

script -q -c "stty cols 150; yum check-update" /path/to/output.txt > /dev/null

150 was an arbitrarily large-enough number to prevent wrapping; adjust that upwards if needed. The -q option inhibits the initial "Script started on $(date)" line, if you use a non-null output filename.

For the original problem, the line would be:

script -q -c "stty cols 150; yum list" /dev/null
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
3

None of the other answers worked for me on RHEL 7.7. So decided to use sed to concatenate next line to all lines that lack @ character.

yum list installed | sed '/^[^@]*$/{N;s/\n//}'
akostadinov
  • 1,048
3

You can use the repoquery command to produce parsable output. For instance:

repoquery '*' --queryformat='%{name} %{evr} %{from_repo}' | column -t

gives:

0ad                0.0.23b-13.fc32  fedora
0ad                0.0.23b-17.fc32  updates
0ad-data           0.0.23b-4.fc32   fedora
0xFFFF             0.8-3.fc32       fedora
2048-cli           0.9.1-9.fc32     fedora
2048-cli-nocurses  0.9.1-9.fc32     fedora
2048-cli-sdl       0.9.1-9.fc32     fedora
2ping              4.3-6.fc32       fedora
2ping              4.5-1.fc32       updates
389-ds-base        1.4.3.18-1.fc32  updates

... and so on.

(Unlike dnf/yum, repoquery doesn't eliminate duplicate packages in its output)

Sam Morris
  • 1,012
  • 1
    When running on CentOS 7, I found that %{from_repo} didn't work. I had to use %{ui_from_repo} instead. Otherwise repoquery was exactly what I was looking for. – jgibson Dec 01 '21 at 03:17
  • Thanks. I always forget when to use repoid/from_repo/ui_from_repo myself! – Sam Morris Dec 01 '21 at 16:04