извлекать только дату из даты и времени - PullRequest
2 голосов
/ 18 марта 2012

Следующий код awk работает как положено.

Дата, например 2012-03-10 ~ 12: 59: 41, округляется до ближайших пяти минут, например, 2012-03-10 ~ 12: 55: 00

Как мне изменить это так, чтобы я мог получить год-месяц-день, например 20120310 (без черты)?

BEGIN {
    # all fields are separated by ^
    FS = "^";
}
{
    # $7 is the date and time in the form yyyy-mm-dd hh:mm:ss.
    # Split at colons to get hours minutes and seconds into a[1]
    # through a[3].  Round minutes to nearest 5.
    split($7, a, ":");
    a[2] = int(a[2] / 5) * 5;
    # print first, second and forth field, then rounded time.
    printf "set %s:%s:%s %s:%02d:00\\r\\n\n", $1, $2, $4, a[1], a[2];
}

1 Ответ

2 голосов
/ 18 марта 2012

Измените разделенную линию на использование gensub():

$ cat file 
1^2^3^4^5^6^2012-03-18~22:09:10

awk 'BEGIN {
         # all fields are separated by ^
         FS = "^";
     }
     {
         # $7 is the date and time in the form yyyy-mm-dd hh:mm:ss.
         # Split at colons to get hours minutes and seconds into a[1]
         # through a[3].  Round minutes to nearest 5.
         split(gensub(/-/,"","g",$7),a,":")
         a[2] = int(a[2] / 5) * 5;
         # print first, second and fourth field, then rounded time.
         printf "set %s:%s:%s %s:%02d:00\\r\\n\n", $1, $2, $4, a[1], a[2];
     }' file

Вывод:

set 1:2:4 20120318~22:05:00\r\n
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...