获取最后修改的项目

时间:2015-03-27 02:21:29

标签: bash awk

输入文件: 名称/路径/ DATE_MODIFIED

omg.txt|/Users/jaro/documents/inc/face/|2015-03-24
lol.txt|/Users/jaro/documents/inc/linked/|2015-03-17
lol.txt|/Users/jaro/documents/inc/twitter/|2015-03-18
lol.txt|/Users/jaro/documents/inc/face/|2015-02-16
wtf.txt|/Users/jaro/documents/inc/face/|2015-03-19
omg.txt|/Users/jaro/documents/inc/twitter/|2015-02-21
omg.txt|/Users/jaro/documents/inc/linked/|2015-03-13
wtf.txt|/Users/jaro/documents/inc/linked/|2015-02-20
lol.txt|/Users/jaro/documents/inc/twitter/|2015-03-03
wtf.txt|/Users/jaro/documents/inc/linked/|2015-02-21
lol.txt|/Users/jaro/documents/inc/face/|2015-02-22
omg.txt|/Users/jaro/documents/inc/twitter/|2015-01-15
omg.txt|/Users/jaro/documents/inc/face/|2015-02-17
wtf.txt|/Users/jaro/documents/inc/face/|2015-02-14
wtf.txt|/Users/jaro/documents/inc/twitter/|2015-04-14
omg.txt|/Users/jaro/documents/inc/linked/|2015-03-15
omg.txt|/Users/jaro/documents/inc/linked/|2015-04-12

我正在尝试查找在给定日期后在给定目录中修改的文件:

e.g。查找路径2015-02-22

/Users/jaro/documents/inc/face/后修改的文件

我的解决方案是尝试将日期从格式YYYY-MM-DD转移到YYYYMMDD,因为我可以将它们比作数字

输出:

omg.txt
wtf.txt

我的代码在这里(虽然它不起作用):

my_date=2015-02-22
wdate=$(echo $my_date | tr -d "-")
my=$(awk -F"|" -v tgt="$path"  '($2==tgt) {print $3}' $input | tr -d "-"| awk -F"|" '($0=="$wdate"){print $1}' $input)

2 个答案:

答案 0 :(得分:1)

如果您可以在没有-的情况下传递日期,则可以删除gsub变量的date

awk -v date="2015-02-22" -F'|' '
    $2=="/Users/jaro/documents/inc/face/" {
        gsub(/-/, "", $3)
        gsub(/-/, "", date)
        if ($3>date && !a[$1]++)
            print $1
}' file

答案 1 :(得分:0)

gawk >可以string comparison ...

my_date=2015-02-22

$ awk -v "my_date=$my_date" -F'|' '$NF>my_date' < input-file.txt
omg.txt|/Users/jaro/documents/inc/face/|2015-03-24
lol.txt|/Users/jaro/documents/inc/linked/|2015-03-17
lol.txt|/Users/jaro/documents/inc/twitter/|2015-03-18
wtf.txt|/Users/jaro/documents/inc/face/|2015-03-19
omg.txt|/Users/jaro/documents/inc/linked/|2015-03-13
lol.txt|/Users/jaro/documents/inc/twitter/|2015-03-03
wtf.txt|/Users/jaro/documents/inc/twitter/|2015-04-14
omg.txt|/Users/jaro/documents/inc/linked/|2015-03-15
omg.txt|/Users/jaro/documents/inc/linked/|2015-04-12