根据星期几对文件进行排序

时间:2015-11-02 14:42:42

标签: linux sorting awk

我有几个文件:

Friday.log
Monday.log
Tuesday.log
Saturday.log
Sunday.log
Thursday.log
Tuesday.log
Wednesday.log

我想将没有.log和内容的文件名放在一个文件中,但是从星期一到星期日的星期几顺序。我有一个命令,将它们放在一起,没有.log但不按顺序:

awk 'FNR==1{sub(/[.][^.]*$/"", FILENAME); print FILENAME} 1' *.log > all.log 

这让我:

Friday
... Friday contents
Monday
... Monday contents
Tuesday
... Tuesday contents
Saturday
... Saturday contents

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

您可以使用%u date参数“播放”以了解任何指定日期的星期几:

$ date -d"last Sunday" +%u
7

但是,由于这个列表非常稳定,为什么不在数组中对其进行硬编码呢?

#!/bin/bash

days=('Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday' 'Sunday')

for day in "${days[@]}"
do
    echo "$day"
    cat "$day.log"
done > all.log

答案 1 :(得分:1)

Perl解决方案:

perl -le 'for $day qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday) {print $day; system("cat $day.log")}'

答案 2 :(得分:0)

我最后刚刚做了:

touch Thursday.log && touch Wednesday.log && touch Tuesday.log && touch Monday.log && ls -t | xargs -i awk 'FNR==1{sub(/[.][^.]*$/"", FILENAME); print FILENAME} 1' {} > all.log