AWK 사용: 오늘부터 5일 전 사이의 날짜가 포함된 행을 검색하기 위해 인쇄 필드를 정의하는 방법은 무엇입니까?

AWK 사용: 오늘부터 5일 전 사이의 날짜가 포함된 행을 검색하기 위해 인쇄 필드를 정의하는 방법은 무엇입니까?

이 목록을 요약하여 5일간의 데이터(오늘부터 5일 전까지)만 포함시키려고 합니다. 사용해 보았 awk으나 소용이 없었습니다. awk를 고수할 필요도 없습니다. 출력은 다음과 같습니다.

awk날짜를 지속적으로 검색하는 코드는 다음과 같습니다 .today's

awk -F " " -v todate="$(date +%Y-%m-%d)" 'todate' output.txt

이것은 내 while 루프 코드입니다.

while read line
    do
    if (("$(date +%Y-%m-%d)" in $line))
        then
            echo $line
done < output.txt

output.txt파일 은 다음과 같습니다 .

2018-05-09 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 16:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 05:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 18:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 07:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 20:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 09:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 22:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-14 11:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 00:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;swlt;;BKP_USGSN03_OLP_MK8;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau;;2025,licensed_sau_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau_lte;;1025,licensed_sau_lte_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;pdp;;2001,licensed_pdp_normal;CXS101289_R73B13(001-00-12)
2018-05-15 13:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 02:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 15:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 04:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 17:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)

답변1

다음 스크립트에서 함수는 mktime파일의 첫 번째 및 두 번째 필드를 기반으로 unix 타임스탬프를 생성합니다. 이는 dateto 매개변수에 지정된 참조 날짜와 비교할 수 있습니다 awk.

awk -v startdate=$(date -d '5 days ago' +%s) '{d=$1 OFS $2; gsub("[-:]", " ", d); t=mktime(d)} t>startdate' file

답변2

암소 비슷한 일종의 영양awk방법:

awk -F'[:-]' -v from_date=$(date -d'-4 days' +%s) \
'mktime(sprintf("%d %d %d %d %d %d", $1, $2, $3, $4, $5, $6)) >= from_date' file.txt

예제 출력:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)

답변3

사용AWK:

옵션 1:

DATE=`date --date='5 days ago' +"%Y-%m-%d"`;awk '/'$DATE'/,EOF { print $0 }' file.txt

옵션 2:

DATE=`date +%Y-%m-%d -d "5 day ago"`;awk '/'$DATE'/,EOF { print $0 }' file.txt

옵션 3:

awk '/2018-05-18/,EOF { print $0 }' file.txt

산출:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)

관련 정보