在AWK比较时 [英] compare time in awk

查看:117
本文介绍了在AWK比较时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查所有文件的修改时间在一个目录。如果任何文件中有从当前系统时间的时间不同,则RED将被打印。否则,绿色印刷。

I want to check the modified time of all the files in one directory. If any file has the time different from the current system time, then RED will be printed. Otherwise , GREEN is printed.

ls -lrt| grep main | awk '{if ($8 != `date +%R` ) print "-->RED"}END{print"-->GREEN"}' 

您可以建议我如何纠正上面的语句。万谢。

May you suggest me how to correct the above statement. Million thanks.

推荐答案

不解析的输出 LS 。你可以做你想做的与统计

与stat -c%Y文件将输出在几秒钟内从1月1日文件的修改时间,1970年日期+%S 将输出当前时间(秒)自1970年1月1日。

stat -c%Y file will output the modification time of a file in seconds since Jan 1, 1970. date +%s will output the current time in seconds since Jan 1, 1970.

所以,你要可以做什么:

So, what you want can be done by:

if [[ $(stat -c%Y main) -ne $(date +%s) ]]
then
    echo "RED"
else
    echo "GREEN"
fi

如果您希望上述文件的列表,并输出红色如果任何时候不同的是:

If you want the above for a list of files, and output RED if any time is different:

to_print=GREEN
for f in *main*
do
    if [[ $(stat -c%Y "$f") -ne $(date +%s) ]]
    then
        to_print=RED
        break
    fi
done
echo $to_print

如果你不使用bash,那么你可以替换 [ ]] 对与 [] $(...)

If you're not using bash, then you can replace [[ ]] pair with [ and ], and $(...) with

`...`

这篇关于在AWK比较时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆