다음과 같은 awk 스크립트가 있다고 가정합니다.
#!/usr/bin/awk -f
BEGIN {
startTimestamp = ARGV[1];
endTimestamp = ARGV[2];
print startTimestamp
print endTimestamp
print startTimestamp + 60 seconds
print endTimestamp + 10 minutes
}
주어진 매개변수에 대해 초 또는 분을 추가하고 싶습니다: startTimestamp
및 endTimestamp
.
예를 들어 다음 스크립트를 호출하여 ./print-timestamps.awk 2018-10-14T13:26:00.000 2018-10-14T13:27:50.000
인쇄하고 싶습니다.
2018-10-14T13:26:00.000
2018-10-14T13:27:50.000
2018-10-14T13:27:00.000
2018-10-14T13:37:50.000
어떻게 해야 하나요?
답변1
GNU에는 다음이 awk
있습니다 .mktime
strftime
$ gawk '{gsub(/[-T:]/," "); print strftime("%Y-%m-%dT%H:%M:%S\n", mktime($0)+60)}'
$ gawk '
function timeadd(t, s){
gsub(/[-T:]/," ", t); return strftime("%Y-%m-%dT%H:%M:%S", mktime(t)+s)
}
BEGIN{
print ARGV[1]
print ARGV[2]
print timeadd(ARGV[1], 60)
print timeadd(ARGV[2], 60 * 10)
}
' 2018-10-14T13:26:00.000 2018-10-14T13:27:50.000
2018-10-14T13:26:00.000
2018-10-14T13:27:50.000
2018-10-14T13:27:00
2018-10-14T13:37:50
그 버전은 timeadd
밀리초도 처리합니다.
$ cat timeadd.awk
function timeadd(t, s){
gsub(/[-T:]/," ", t)
t = mktime(t) + s + substr(t, index(t, "."))
s = substr(sprintf("%.3f", t - int(t)), 2)
return strftime("%Y-%m-%dT%H:%M:%S" s, t)
}
{ print timeadd($1, $2); }
$ gawk -f /tmp/timeadd.awk
2018-10-19T13:26:58.815 4.237
2018-10-19T13:27:03.052
2018-10-19T13:26:58.122 -3.822
2018-10-19T13:26:54.300
답변2
GNU를 date
사용할 수 있나요? 노력하다
awk '
BEGIN {
startTimestamp = ARGV[1];
endTimestamp = ARGV[2];
print startTimestamp
sub (/T/, " ", startTimestamp)
print endTimestamp
sub (/T/, " ", endTimestamp)
system ("date +\"%FT%T\" -d\"" startTimestamp " 60 seconds\"")
system ("date +\"%FT%T\" -d\"" endTimestamp " 60 minutes\"")
} ' 2018-10-14T13:26:00.000 2018-10-14T13:27:50.000
2018-10-14T13:26:00.000
2018-10-14T13:27:50.000
2018-10-14T13:27:00
2018-10-14T14:27:50