From adb6338bb5cd01f4b733a96354fa6775741224e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Tue, 23 Apr 2024 12:58:17 +0100 Subject: [PATCH] doc: sort: give example for sorting on the last field * doc/coreutils.texi (sort invocation): Give a DSU example for sorting names which may have a variable number of fields. Addresses https://bugs.gnu.org/70532 --- doc/coreutils.texi | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 2c6c8b68b..3762c6f96 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -5151,13 +5151,23 @@ Use the common DSU, Decorate Sort Undecorate idiom to sort lines according to their length. @example -awk '@{print length, $0@}' /etc/passwd | sort -n | cut -f2- -d' ' +getent passwd |@/ +awk '@{print length, $0@}' | sort -n | cut -f2- -d' ' @end example In general this technique can be used to sort data that the @command{sort} command does not support, or is inefficient at, sorting directly. @item +Use the same DSU idiom as above to sort lines by their last field, +and in this specific example the presented lines are users' full names. + +@example +getent passwd | grep -v nologin | cut -d: -f5 | grep ' ' |@/ +awk '@{print $NF, $0@}' | sort -k1,1 | cut -f2- -d' ' +@end example + +@item Shuffle a list of directories, but preserve the order of files within each directory. For instance, one could use this to generate a music playlist in which albums are shuffled but the songs of each album are -- 2.11.4.GIT