awk:按日期查找最旧和最新的文件

awk:按日期查找最旧和最新的文件

我正在使用awk script打印根据日期(第 6 个、第 7 个和第 8 个字段)创建的最旧和最新文件。
我的样本列表,日期格式为(YYYY/MM/DD):

file was created 2020/10/10 20:18:42 its name is output1
file was created on 2020/09/10 12:13:22 its name is foobar.awk
file was created on 2020/10/10 20:12:43 its name is output2
file was created on 2020/12/10 18:11:38 its name is foobar.bash
file was created on 2020/12/10 22:32:13 its name is output.txt

它应该是什么样子:

Oldest file date :
2020/09/10 12:13:22
file name: foobar.awk

Newest file date :
2020/12/10 22:32:13
file name: output,txt

注意:我只能使用awk“我只想打印最旧和最新的文件”。
另外:我不会对文件进行排序,只是简单地打印最旧和最新的文件。

答案1

假设 GNU awk:

gawk '
    {created[$NF] = $5 " " $6}
    END {
        PROCINFO["sorted_in"] = "@val_str_asc"
        for (f in created) {print "oldest:", f, created[f]; break}
        PROCINFO["sorted_in"] = "@val_str_desc"
        for (f in created) {print "newest:", f, created[f]; break}
    }
' file

参考:控制数组遍历, 和通过 gawk 使用预定义的数组扫描顺序

答案2

用于stat获取文件及其修改时间,awk获取最小/最大而不排序和格式化输出。

stat -c "%Y %n" * | awk '
    min == "" || min > $1 {min = $1; fmin = substr($0, index($0, FS)+1)}
    max == "" || max < $1 {max = $1; fmax = substr($0, index($0, FS)+1)}
    END {
        form = "%Y/%m/%d %H:%M:%S"
        printf "Oldest\nDate: %s\nFile: %s\n", strftime(form, min), fmin
        printf "Newest\nDate: %s\nFile: %s\n", strftime(form, max), fmax
    }'

示例输出:

Oldest
Date: 2020/08/29 01:52:23
File: test.java
Newest
Date: 2020/10/12 06:52:21
File: test.sh

笔记:

  • 上面的代码将空格处理到文件中,而不是换行符。
  • 我认为 awk strftime 仅限 GNU

相关内容