sorting pathnames by basename
Bob Bell
bobbell at zk3.dec.com
Mon Aug 19 17:45:35 EDT 2002
On Mon, Aug 19, 2002 at 03:46:40PM -0400, Michael O'Donnell <mod+gnhlug at std.com> wrote:
>
> Given a list of pathnames, I'd like to be able to
> sort that list by the basename of each file in the
> list, ie. the pathname
>
> q/r/s/t/u/v/aaa
>
> ...would sort ahead of
>
> aaaa/aaaa/bbb
>
> ...because the basename 'aaa' sorts lexicographically
> ahead of 'bbb'.
>
> My current approach is a bit clunky but works as long as
> the pathnames in question contain no spaces, thus:
>
> sed -e 's;/\([^/]*\)$; \1;' | sort -bfd +1 | sed -e 's; ;/;'
>
> ...which replaces the last slash in each pathname with
> a space, sorts that list of dual-field space-delimited
> records keyed on the second field, and then replaces
> the slashes. Suggestions for improvement welcome.
'/' is an illegal character in a filename, right?
sed -e 's;\(\(.*\)/\)*\(.*\);\3/\1\3;' | sort -fdt/k1 | sed -e 's;[^/]*/\(.*\);\1;'
--
Bob Bell <bobbell at zk3.dec.com>
-------------------------------------------------------------------------
"Digital files cannot be made uncopyable, any more than water can
be made not wet."
-- Bruce Schneier, CRYPTO-GRAM, May 15, 2001
More information about the gnhlug-discuss
mailing list