And if you don't like pipes: python -c'import random,apt;print random.choice(apt.cache.Cache().keys())' It even ends up shorter than the original perl: > dpkg --get-selections | perl -E '@p = map { s/\s.*//r } <>; say $p[rand(+ at p)]' Douglas