How to determine the "biggest number" so that sort -n will always put it at the end? I'm thinking of something along Inf in some languages but I'm not sure if anything like this exists for sort.
The background is that I'm sorting list of potentially non-existent paths by age so that
existent go first, from newest to oldest,
non-existent go last.
I'm using decoration approach and trying to put penalty on those unborns:
INFINITY=99999999999999 # ...close enough, right?
age_of() {
if [ -e $1 ];
then
local ctime=$(stat -c %Z "$1" 2>/dev/null)
local now=$(date +%s)
echo $(($now - $ctime))
else
echo $INFINITY
fi
}
cat - \
| while read path;
do
echo "$(age_of $path) $path"
done \
| sort -n \
| cut -d\ -f 2-
but obviously the comment is pretty naive; it's just a matter of time when even 99999999999999 will not be close enough. ;)
So is there a better value for INFINITY?
age_ofreturned the same number. I'm rather looking for something that will always be after all numbers ... if it makes sense.sort -g(a GNUism, but then you're using GNU stat) recognises"inf".perl -lne '$age = -M; $age = "inf" unless defined $age; print "$age $_"' | sort -g