내 NAS는 아래 형식으로 로그 파일을 생성합니다.
<30>1 2014-07-21T05:02:10+02:00 ABC-NAS qlogd 6432 - - qlogd[6432]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-PC] finished successfully
<30>1 2014-07-21T05:02:27+02:00 ABC-NAS qlogd 6432 - - qlogd[6432]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-HTPC] finished successfully
<28>1 2014-07-21T05:10:59+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-NAS] is abnormal shutdown
<30>1 2014-07-21T06:00:15+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC Metadata] started
<30>1 2014-07-21T06:00:27+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC Metadata] finished successfully
BASH 스크립트를 사용하여 다음 내용만 포함하는 텍스트 파일을 만들고 싶습니다.
Monday 21 July 2014 - 04:10 AM
Glacier Backup job [Backup ABC-PC] finished successfully
Monday 21 July 2014 - 07:02 AM
Glacier Backup job [Backup ABC-HTPC] finished successfully
등등.
나는 cat과 grep을 사용해 보았지만 곧 너무 복잡해졌습니다. 누가 나를 올바른 방향으로 도와줄 수 있나요?
답변1
여기 매우 간단한 무차별 대입 bash 스크립트가 있습니다.
cat log | cut -d " " -f 2,20- | while read -r date message; do
echo $date
echo $message
done
샘플 출력
2014-07-21T05:02:10+02:00
[Glacier] Backup job [Backup ABC-PC] finished successfully
2014-07-21T05:02:27+02:00
[Glacier] Backup job [Backup ABC-HTPC] finished successfully
더 많은 기능을 원한다면 파이썬을 배우라고 강력히 권합니다.