
Advance warning: this mail ended up being pretty academical in nature… * Jonathan Purvis <jon(a)purvis.co.nz> [2005-08-12 02:50]:
But with your idea about using IFS, i managed to get this, which does work:
Filenames can legally contain newlines. (Yes, it’s rare, but it’s also possible.) The canonical way to go about this is passing `-print0` to find(1) so it terminates filenames with a NUL rather than a LF, and spliting on NULs in whatever processes the filenames next.
echo `find . -name '*E1M*' | awk '{print $2,$0}' FS='[()]' | sort | sed -e 's/[^ ]* \(.*\)/\1/' -e 's/\([ ()]\)/\\\\\1/g'`
After squinting at this command for a while, I think what you’re doing is massaging the filenames to properly sort them, then putting them back together, and finally escaping shell metachars. Is that right? This is really a task sort(1) should be up to… unfortunately it’s not, since it can’t use mulitple delimiter characters and can’t cope with NUL-separated input either (even though it can produce it, sigh). awk is useless, too. Escaping metachars wouldn’t be necessary if you kept the shell out of the picture, ie no backticks to invoke mplayer, but that’s not doable with sed(1) in the picture, since that can’t split on NULs at all… yuck. I don’t see a way to do with *well* without Perl. find . -name '*E1M*' -print0 | perl -naF'\0' -0777l0e' @k = map join( "", ( split /[()]/ )[2, 0] ), @F; print for @F[ sort { $k[$a] cmp $k[$b] } 0 .. $#k ] ' | xargs -0 mplayer Quite a mouthful. :-( This really is a task that sort(1) *should* be fully up to… splitting on NULs and being able to use multiple different as delimiter characters would make it a simple matter of saying something like find . -name '*E1M*' -print0 | sort -0z -t \( -t \) -k 3,1 | xargs -0 mplayer Alas. Regards, -- Aristotle Pagaltzis // <http://plasmasturm.org/>