使用 bash 排序 rfc 日期
sortf rfc dates using bash
如何对包含 rfc 日期的文本文件进行排序?
例如:
Sat, 1 Aug 2015 01:48:56 +0200
Sat, 1 Aug 2015 01:25:40 +0200
Sun, 19 Jul 2015 14:47:29 -0300
Sat, 13 Sep 2014 12:13:51 -0300
谢谢!
通过 date 命令传递每个日期,将它们从
epoch 后接原字符串,排序,去掉添加的秒数:
while read date
do date --date "$date" +"%s $date"
done |
sort -n -k 1,1 |
sed 's/[^ ]* //'
与@meuh 类似的想法,但只调用 perl 而不是为每一行调用一次 date:
perl -MTime::Piece -lne '
push @dates, [Time::Piece->strptime($_, "%a, %e %b %Y %T %z"), $_]
} {
print join "\n",
map {$_->[1]}
sort {$a->[0] <=> $b->[0]}
@dates
' dates.txt
Sat, 13 Sep 2014 12:13:51 -0300
Sun, 19 Jul 2015 14:47:29 -0300
Sat, 1 Aug 2015 01:25:40 +0200
Sat, 1 Aug 2015 01:48:56 +0200
如果您的 sort
版本实现了 -M
选项(按月排序)(OS X sort
实现),您可以对字符串进行排序三个相关领域:
# First, sort on the 4th field numerically (year)
# In the same year, sort on the 3rd field by month
# In the same year and month, sort on the 2nd field numerically (day)
sort -k4,4n -k3,3M -k2,2n dates.txt
如何对包含 rfc 日期的文本文件进行排序?
例如:
Sat, 1 Aug 2015 01:48:56 +0200 Sat, 1 Aug 2015 01:25:40 +0200 Sun, 19 Jul 2015 14:47:29 -0300 Sat, 13 Sep 2014 12:13:51 -0300
谢谢!
通过 date 命令传递每个日期,将它们从 epoch 后接原字符串,排序,去掉添加的秒数:
while read date
do date --date "$date" +"%s $date"
done |
sort -n -k 1,1 |
sed 's/[^ ]* //'
与@meuh 类似的想法,但只调用 perl 而不是为每一行调用一次 date:
perl -MTime::Piece -lne '
push @dates, [Time::Piece->strptime($_, "%a, %e %b %Y %T %z"), $_]
} {
print join "\n",
map {$_->[1]}
sort {$a->[0] <=> $b->[0]}
@dates
' dates.txt
Sat, 13 Sep 2014 12:13:51 -0300
Sun, 19 Jul 2015 14:47:29 -0300
Sat, 1 Aug 2015 01:25:40 +0200
Sat, 1 Aug 2015 01:48:56 +0200
如果您的 sort
版本实现了 -M
选项(按月排序)(OS X sort
实现),您可以对字符串进行排序三个相关领域:
# First, sort on the 4th field numerically (year)
# In the same year, sort on the 3rd field by month
# In the same year and month, sort on the 2nd field numerically (day)
sort -k4,4n -k3,3M -k2,2n dates.txt