Jeff
Jeff

Reputation: 4423

How to format bash output depending on string length?

Right now, I am running the following command:

rpm -qa --queryformat '%{name}\t%{installtime:date}\n' | sort -nr

and getting some output like this:

dhclient        Fri 07 Feb 2014 01:37:47 PM EST
device-mapper-persistent-data   Fri 07 Feb 2014 01:27:37 PM EST
device-mapper-libs      Fri 07 Feb 2014 01:34:44 PM EST
device-mapper   Fri 07 Feb 2014 01:34:46 PM EST
device-mapper-event-libs        Fri 07 Feb 2014 01:34:48 PM EST
device-mapper-event     Fri 07 Feb 2014 01:34:50 PM EST
dbus-libs       Fri 07 Feb 2014 01:25:28 PM EST
dbus-glib       Fri 07 Feb 2014 01:33:48 PM EST
db4-utils       Fri 07 Feb 2014 01:30:05 PM EST
db4     Fri 07 Feb 2014 01:24:58 PM EST
dash    Fri 07 Feb 2014 01:30:19 PM EST
cyrus-sasl-lib  Fri 07 Feb 2014 01:25:48 PM EST

(note the odd tabs)

How do I tell the command I want it to output it into a table with common spacing instead of specifying the number of tabs?

Extra Question:

What I'm trying to do is just find out what has been installed and when so I can uninstall everything that I installed recently. How do I do that better than what I'm doing?

Upvotes: 0

Views: 94

Answers (1)

John Kugelman
John Kugelman

Reputation: 361585

rpm -qa --queryformat '%-40{name} %{installtime:date}\n' | sort -nr
                        ^^^

This will left-align the name and pad it to 40 characters.

If you want to order by time, you could print the numeric time first so it's easy to sort by.

$ rpm -qa --queryformat '%-10{installtime}   %{installtime:date}   %{name}\n' | sort -n
...
1375369678   Thu 01 Aug 2013 11:07:58 AM EDT   xorg-x11-util-macros
1375886901   Wed 07 Aug 2013 10:48:21 AM EDT   libdc1394
1378148462   Mon 02 Sep 2013 03:01:02 PM EDT   gnome-system-monitor
1384526666   Fri 15 Nov 2013 09:44:26 AM EST   perl-File-Next
1384526667   Fri 15 Nov 2013 09:44:27 AM EST   ack
1385065567   Thu 21 Nov 2013 03:26:07 PM EST   trousers
1385065568   Thu 21 Nov 2013 03:26:08 PM EST   tpm-tools
1387405750   Wed 18 Dec 2013 05:29:10 PM EST   libusb1

Upvotes: 5

Related Questions