2012-03-18 4 views
2

다음 awk 코드가 예상대로 작동합니다.날짜와 시간의 날짜 만 추출하십시오.

2012-03-10 ~ 12 : 59 : 41과 같은 날짜는 가장 가까운 5 분으로 반올림됩니다. 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]; 
} 
+0

분할 ($ (7), "-"); #가 작동하지만, 월 03은 [2] = int (a [2])를 사용했기 때문에 3으로 변환되었습니다. 텍스트로 변환하려면 어떻게해야합니까? 이 a [2] = var (a [2]); 작동하지 않습니다. – shantanuo

답변

2
이 사용하도록 분할 줄을 변경

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 
+0

수정. 빠른 답변 주셔서 감사합니다. – shantanuo

+0

날짜 부분 만 필요합니다. 20120318 ~ 22 : 05 : 00 충분하지 않습니다. – shantanuo

관련 문제