比较awk中的变量

比较awk中的变量,awk,Awk,我的文件在其第6个字段中有时间戳,如下所示:Mon Jul 7 14:53:16 PDT 2014 我想从该文件中获取其第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 20

我的文件在其第6个字段中有时间戳,如下所示:Mon Jul 7 14:53:16 PDT 2014

我想从该文件中获取其第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
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
所需输出

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命令中调用像date这样的shell命令,我想知道如何处理这个问题。我希望它足够清楚。

这是一个想法的草图。注意它是特定于呆呆的

# 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: 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
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

$(yest)
应该是
“$yest”
。文件中字段6的格式是什么
$yest
将类似于2014年7月6日星期日16:26:09 CDT,我不知道您希望如何将其与
awk-v yest=$yest…
进行比较。您的变量引用语法已禁用:使用
“$yest”
“${yest}”
-
$(yest)
不是变量引用,但是命令替换。你认为这和时间戳的格式有关吗?可以显示文件中的示例行吗?添加一个显示两个值的调试行,并用边界字符包围,例如,
print“a=|”a“|\tyest=|“yest”|“
。其他意见也应遵守;-)祝你好运。你应该提到你的剧本是针对gawk的。OP可能未使用GNU awk。是。时间函数(mktime()/systime()、gensub()、PROCINFO[]、asort/asorti()和真正的多维数组(arr[i][j]语法vs arr[i,j])是人们使用的典型的gawk特定功能。我不知道有哪张桌子是最新的。@EdMorton我喜欢这样!更不用说打字了。我会保持原样,但我会记住这一点。谢谢。@Jotne这是另一种有效的方法,但如何更好?如果要避免重复,函数m(n){return(match(“janfebmarapramayjunjulaugsepoctnovdec”,n)+2)/3}BEGIN{print m(“Jun”)}
将是最简单、最容易添加检查以在一个地方处理无效月份名称的函数。也许有性能上的差异?idk…@EdMorton确实忘记了
函数
:)