로그가 포함된 큰 텍스트 파일이 있습니다. 다음 awk 명령을 실행하면 다음과 같은 출력이 나타납니다.
grep TEST-FIELD test.log | awk '{print $23,$24,$4,$6,$7,$11,$12,$13}'
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
날짜 열($23)의 출력을 다음으로 변경해야 합니다.
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
다음 명령을 실행해 보았지만 올바른 값이 전혀 인쇄되지 않았습니다.
grep TEST-FIELD test.log | awk -F"/" '{OFS="/"; $23=strftime("%Y-%m-%d", $23); print $24,$4,$6,$7,$11,$12,$13}'
AWK만 사용하여 올바른 출력을 얻는 방법
답변1
원본 awk를 다음으로 변경하십시오.
awk '{gsub ("/", "-", $23); print $23,$24,$4,$6,$7,$11,$12,$13}'
답변2
주문하다
awk '{x=substr($1,1,4)"-"substr($1,6,2)"-"substr($1,9,2);$1="";print x$0}' filename
산출
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx