我想用user()和
作为参数$./tool.sh --born-since <dateA> --born-until <dateB>
并从文件中打印出这两个日期之间的行。例如:
933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Hồ ChÃ|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
所以,我使用这样的awk命令:
awk -F'|' '{print $4} [ file ... ]
采取日期..我如何使用awk将日期从txt变为秒形式?
答案 0 :(得分:2)
如果日期变量采用相同的格式,您可以将所有内容转换为数字并使用比较。
awk -F'|' -v from=$dateA -v to=$dateB '{gsub("-","",$5);
gsub("-","",from); gsub("-","",to)}
from <= $5 && $5 <= to' file
请注意,它是您文件中的第五个字段。
答案 1 :(得分:1)
如果DATESTRING符合格式&#34; / bin / date&#34;您可以拨打/bin/date +"%s" --date="DATESTRING"
到system()
。理解,或者你使用内部的mktime()函数。但是你需要根据awk(1)分割你的日期:
mktime(datespec)
Turn datespec into a time stamp of the same form as returned by systime(), and return the result. The datespec is a string of
the form YYYY MM DD HH MM SS[ DST]. The contents of the string are six or seven numbers representing respectively the full year
including century, the month from 1 to 12, the day of the month from 1 to 31, the hour of the day from 0 to 23, the minute from 0
to 59, the second from 0 to 60, and an optional daylight saving flag. The values of these numbers need not be within the ranges
specified; for example, an hour of -1 means 1 hour before midnight. The origin-zero Gregorian calendar is assumed, with year 0
preceding year 1 and year -1 preceding year 0. The time is assumed to be in the local timezone. If the daylight saving flag is
positive, the time is assumed to be daylight saving time; if zero, the time is assumed to be standard time; and if negative (the
default), mktime() attempts to determine whether daylight saving time is in effect for the specified time. If datespec does not
contain enough elements or if the resulting time is out of range, mktime() returns -1.
因此,您需要准备日期字段以使用文档中提供的表单。
split($5, D, "-");
DS = sprintf("%4d %2d %2d 00 00 00", D[1], D[2], D[3]);
T = mktime(DS);
应该做的。