我的文件在第6个字段中有时间戳,如下所示: 2014年7月7日星期一14:53:16
我希望从第6个字段值在过去24小时内的此文件中获取所有这些行。
示例输入:
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 4 10:06:33 PDT 2014
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 5 10:06:33 PDT 2014
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 7 07:06:33 PDT 2014
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 7 08:06:33 PDT 2014
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 7 09:06:33 PDT 2014
abc -> /aa/bbb, hello, /home/user/blah.pl, 516, usc, Mon Jul 7 10:06:33 PDT 2014
字段分隔符是逗号。
示例代码 但它没有按预期工作:
awk 'BEGIN {FS = ","};
{ a=$6;
aint=a +"%y%m%d%H%M%S";
yestint=$(date --date='1 day ago' +"%y%m%d%H%M%S");
if (aint>yestint)
print aint;
}' /location/canzee/textfile.txt
示例输出 我得到这样的输出:
awk: cmd. line:4: yestint=$(date --date=1
awk: cmd. line:4: ^ syntax error
awk: cmd. line:5: (END OF FILE)
awk: cmd. line:5: syntax error
所需输出
Mon Jul 7 07:06:33 PDT 2014
Mon Jul 7 08:06:33 PDT 2014
Mon Jul 7 09:06:33 PDT 2014
Mon Jul 7 10:06:33 PDT 2014
如果我不能在awk命令中调用类似日期的shell命令,我想知道如何解决这个问题。我希望它足够清楚。
答案 0 :(得分:1)
这是一个想法的草图。请注意它是特定的gawk。
# An array to convert abbreviated month names to numbers.
BEGIN {m["Jan"]=1; m["Feb"]=2; m["Mar"]=3; m["Apr"]=4; m["May"]=5; m["Jun"]=6
m["Jul"]=7; m["Aug"]=8; m["Sep"]=9; m["Oct"]=10; m["Nov"]=11; m["Dec"]=12;}
# later in your script
{
# systime() gives the number of seconds since the "epoch".
# Subtract 24-hours-worth of seconds from it to get "yesterday".
# (Note that this is yesterday at a specific time, which may not
# really be what you want.)
yest = systime() - 24 * 60 * 60;
a = "Mon Jul 7 14:27:56 PDT 2014" # or however a gets its value
# Split the fields of a into the array f (splitting on spaces).
split(a, f, " ");
# Split the fields of f[4] (the time) into the array t (splitting on colons).
split(f[4], t, ":")
# mktime() converts a date specification into seconds since the epoch.
# The datespec format is: 2014 7 7 14 27 56 [optional dst flag]
# If the daylight savings time flag is left out the system tries to determine
# whether or not dst is in effect.
tm = mktime(f[6] " " m[f[2]] " " f[3] " " t[1] " " t[2] " " t[3])
#Compare the seconds since epochs.
if (tm > yest)
...
}
在您的程序环境中,可能会这样做:
awk '
BEGIN {
m["Jan"]=1; m["Feb"]=2; m["Mar"]=3; m["Apr"]=4; m["May"]=5; m["Jun"]=6
m["Jul"]=7; m["Aug"]=8; m["Sep"]=9; m["Oct"]=10; m["Nov"]=11; m["Dec"]=12;
FS = "[[:space:]]*,[[:space:]]*"
yest = systime() - 24 * 60 * 60;
}
{
split($6, f, " ")
split(f[4], t, ":")
tm = mktime(f[6] " " m[f[2]] " " f[3] " " t[1] " " t[2] " " t[3])
if (tm > yest)
print $6;
}
' /location/canzee/textfile.txt