I was looking through the various man pages (pkg_* especially pkg_info(1)) trying to find an option where I could get a sorted list by size, of installed packages. pkg_info(1) has the "-s" option to output size, but no sorting.
Clearly some text munging was needed, with the use of pipes. Here's what I came up with:
Code:
pkg_info -a -s | sed '/^$/d' | paste - - | sort -k 5 -n -r \
| sed 's/^Information for inst://g' | awk '{print $3 "\t\t" $1};' \
| numfmt --to=si
The first two parts get a list of installed packages plus size, removing empty lines, then I use paste(1) to join adjacent lines, do a reverse numeric sort on the 5th field, remove unneeded stuff, then print lines with fields in "size package" order.
numfmt(1) converts bytes to human-readable form and can be omitted if you don't have GNU coreutils installed, in which case the awk(1) command could be extended to do the conversion.
Pipes are one of the core components of the UNIX philosophy / way-of-doing-things, and one of the main reasons I love the OS family, particularly OpenBSD.