我有带有 n 个文件的数组 INPUTFILES
INPUTFILES=( file_0 ... files_n-1 )
我需要按文件中的第一行按数组顺序对它们进行排序。
文件如下所示:
2012.09.20 17:10
2012.11.21 00:10
2012.12.22 15:10
2012.12.23 15:10
我已经有比较两个文件的功能:
IsSooner () {
ONEFIRST=$( head -1 "${1}" )
ONELAST=$( tail -1 "${1}" )
TWOFIRST=$( head -1 "${2}" )
TWOLAST=$( tail -1 "${2}" )
TIMEFORMAT='Y.%m.%d %H:%M:'
perl <<EOF
use strict;
use warnings;
use Time::Piece;
open STDERR, "> /dev/null";
my @dates1 = ("${ONEFIRST}","${ONELAST}");
my @range1 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates1;
my @dates2 = ("${TWOFIRST}","${TWOLAST}");
my @range2 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates2;
if ( \$range1[0] < \$range2[0] ) {
exit 0;
}
exit 1;
EOF
[ $? -eq 0 ] && {
return 0
}
return 1
}
较早的将是文件中的第一个日期,数组中较小的索引将具有。
如果需要,可以使用 BASH 中的解决方案。
更新 我事先不知道日期的格式。我只知道它将采用 strftime(3c) 格式。