awk를 사용하여 syslog 데이터 구문 분석 및 형식 지정

awk를 사용하여 syslog 데이터 구문 분석 및 형식 지정

생성기에서 syslog 데이터를 가져오고 있는데 줄 시작 부분의 날짜와 시간과 파일 끝 부분의 작업을 추출하고 싶습니다.

지금까지 내가 가진 것은 이것뿐입니다.

awk -F ' ' '{print $1,$2,$3,":",$NF}' test.log

내 출력에 불완전한 로그 메시지가 있습니다.

Sep 17 20:47:02 : Warning
Sep 20 12:02:20 : Started
Sep 20 12:09:31 : Level
Sep 20 12:10:27 : Level
Sep 20 12:10:28 : Stopped
Sep 23 09:19:03 : Auto
Sep 23 09:19:04 : Cleared
Sep 23 09:19:13 : Auto
Sep 30 12:33:48 : Auto
Sep 30 12:33:55 : Started
Sep 30 12:41:14 : Level
Sep 30 12:41:56 : Stopped
Sep 30 12:41:58 : Cleared
Oct 3 14:00:52 : Started
Oct 3 14:15:17 : Stopped
Oct 3 14:20:39 : Auto
Oct 15 12:02:55 : Started
Oct 15 12:12:50 : Stopped

이것이 실제 날짜와 시간 및 전체 작업 메시지를 인쇄하려는 것입니다.

Date Time Fuel has High Warning
Date Time Genset Started
Date Time  Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Date Time  Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level
Date Time  Genset Stopped
Date Time  Genset Not In Auto
Date Time  All Faults Cleared
Date Time  Genset in Auto
Date Time  Genset Not In Auto
Date Time  Genset Started
Date Time  Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Date Time  Genset Stopped
Date Time  All Faults Cleared
Date Time  Genset Started
Date Time  Genset Stopped
Date Time  Genset in Auto
Date Time  Genset Started
Date Time  Genset Stopped

각 줄에는 고정된 길이가 없습니다.

테스트 파일입니다

Sep 17 20:47:02 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#011#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#0249/17/2023 8:47:01 PM0#023#006#013+#006#001#004#001��s#001#002#000#004#004Fuel0#023#006#013+#006#001#004#001��s#001#004#000#004#00445940#026#006#013+#006#001#004#001��s#001#003#000#004#007Warning0$#006#013+#006#001#004#001��s#001#005#000#004#025Fuel has High Warning
Sep 20 12:02:20 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#012#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/20/2023 12:02:23 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451430#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Started
Sep 20 12:09:31 192.168.22.60 0�#001(#002#001#001#004#005AE4ML��#001#032#002#001#013#002#001#000#002#001#0000�#001#0150#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/20/2023 12:09:34 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#022#006#013+#006#001#004#001��s#001#004#000#004#0031460#026#006#013+#006#001#004#001��s#001#003#000#004#007Warning0s#006#013+#006#001#004#001��s#001#005#000#004dEngine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 20 12:10:27 192.168.22.60 0�#001##002#001#001#004#005AE4ML��#001#025#002#001#014#002#001#000#002#001#0000�#001#0100#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/20/2023 12:10:30 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#022#006#013+#006#001#004#001��s#001#004#000#004#0031510#027#006#013+#006#001#004#001��s#001#003#000#004#010Shutdown0m#006#013+#006#001#004#001��s#001#005#000#004^Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level
Sep 20 12:10:28 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#015#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/20/2023 12:10:30 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451440#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Stopped
Sep 23 09:19:03 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#016#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#0249/23/2023 9:19:05 AM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451380#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0!#006#013+#006#001#004#001��s#001#005#000#004#022Genset Not In Auto
Sep 23 09:19:04 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#017#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#0249/23/2023 9:19:07 AM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00472190#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0!#006#013+#006#001#004#001��s#001#005#000#004#022All Faults Cleared
Sep 23 09:19:13 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#020#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#0249/23/2023 9:19:16 AM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00456640#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset in Auto
Sep 30 12:33:48 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#021#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/30/2023 12:33:50 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451380#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0!#006#013+#006#001#004#001��s#001#005#000#004#022Genset Not In Auto
Sep 30 12:33:55 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#022#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/30/2023 12:33:57 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451430#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Started
Sep 30 12:41:14 192.168.22.60 0�#001(#002#001#001#004#005AE4ML��#001#032#002#001#023#002#001#000#002#001#0000�#001#0150#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/30/2023 12:41:17 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#022#006#013+#006#001#004#001��s#001#004#000#004#0031460#026#006#013+#006#001#004#001��s#001#003#000#004#007Warning0s#006#013+#006#001#004#001��s#001#005#000#004dEngine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 30 12:41:56 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#024#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/30/2023 12:41:59 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451440#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Stopped
Sep 30 12:41:58 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#025#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000$#006#013+#006#001#004#001��s#001#001#000#004#0259/30/2023 12:42:01 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00472190#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0!#006#013+#006#001#004#001��s#001#005#000#004#022All Faults Cleared
Oct  3 14:00:52 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#026#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#02410/3/2023 2:00:55 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451430#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Started
Oct  3 14:15:17 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#027#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#02410/3/2023 2:15:20 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451440#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Stopped
Oct  3 14:20:39 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#030#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000##006#013+#006#001#004#001��s#001#001#000#004#02410/3/2023 2:20:42 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00456640#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset in Auto
Oct 15 12:02:55 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#031#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000%#006#013+#006#001#004#001��s#001#001#000#004#02610/15/2023 12:02:59 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451430#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Started
Oct 15 12:12:50 192.168.22.60 0��#002#001#001#004#005AE4ML���#002#001#032#002#001#000#002#001#0000��0#015#006#010+#006#001#002#001#001#003#000C#001#0000#030#006#012+#006#001#006#003#001#001#004#001#000#006#012+#006#001#004#001��s#001#0000%#006#013+#006#001#004#001��s#001#001#000#004#02610/15/2023 12:12:54 PM0#033#006#013+#006#001#004#001��s#001#002#000#004#01412615_lussie0#023#006#013+#006#001#004#001��s#001#004#000#004#00451440#032#006#013+#006#001#004#001��s#001#003#000#004#013Information0#035#006#013+#006#001#004#001��s#001#005#000#004#016Genset Stopped

답변1

데이터에 잘못된 문자 집합 인코딩을 사용하고 있는 것 같습니다. (이것은 데이터의 �� 문자를 설명할 수 있습니다.) 처리 중인 출력에 대한 참조 문서를 연구하여 출력이 어떻게 해석되어야 하는지 확인해야 합니다.

그러나 "기본적으로 정확"하다는 점을 고려하면 필드 구분 기호는 기본적으로 #다음과 같으므로 다음과 같이 시작할 수 있습니다.

awk -F'#' '{ print $1, $NF }'                                                                                 

샘플 출력

Sep 17 20:47:02 192.168.22.60 0�� 025Fuel has High Warning
Sep 20 12:02:20 192.168.22.60 0�� 016Genset Started
Sep 20 12:09:31 192.168.22.60 0� 004dEngine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 20 12:10:27 192.168.22.60 0� 004^Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level

이제 공백으로 구분된 첫 번째 필드 세트를 분할하고 경험상 "마지막" 필드의 시작 부분에서 세 문자를 제거해야 합니다.

awk -F'#' '{ split($1, a, " "); print a[1], a[2], a[3], substr($NF,4,length($NF)) }'

샘플 출력

Sep 17 20:47:02 Fuel has High Warning
Sep 20 12:02:20 Genset Started
Sep 20 12:09:31 dEngine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 20 12:10:27 ^Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level

d이를 유지하기 위해 세 번째 및 네 번째 줄에서 선행 및를 제거해야 하는 방법과 이유에 대한 명확한 증거가 없습니다 .^

답변2

POSIX awk를 사용하십시오.

$ awk '{sub(/[0-9]+\..*(#[0-9]{3}){4}[^[:upper:]]*/,"")} 1' file
Sep 17 20:47:02 Fuel has High Warning
Sep 20 12:02:20 Genset Started
Sep 20 12:09:31 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 20 12:10:27 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level
Sep 20 12:10:28 Genset Stopped
Sep 23 09:19:03 Genset Not In Auto
Sep 23 09:19:04 All Faults Cleared
Sep 23 09:19:13 Genset in Auto
Sep 30 12:33:48 Genset Not In Auto
Sep 30 12:33:55 Genset Started
Sep 30 12:41:14 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 30 12:41:56 Genset Stopped
Sep 30 12:41:58 All Faults Cleared
Oct  3 14:00:52 Genset Started
Oct  3 14:15:17 Genset Stopped
Oct  3 14:20:39 Genset in Auto
Oct 15 12:02:55 Genset Started
Oct 15 12:12:50 Genset Stopped

#NNN위의 내용은 삭제하려는 섹션의 끝에 항상 최소 4개의 문자열 시퀀스가 ​​있고 #NNN인쇄하려는 나머지 섹션에는 4개의 문자열이 없지만 #다른 컨텍스트에서는 s가 포함될 수 있다고 가정합니다. .

d또한 인쇄되는 작업의 일부는 항상 대문자로 시작한다고 가정합니다. 그렇지 않으면 예상 출력을 기반으로 입력의 3번째 줄 시작 부분에서 제거 해야 한다는 것을 알 수 없기 때문입니다 .dEngine ...

-E예를 들어 ERE를 지원하는 sed(예: GNU 또는 BSD sed)를 사용하여 sed로 동일한 작업을 수행할 수 있습니다.

$ sed -E 's/[0-9]+\..*(#[0-9]{3}){4}[^[:upper:]]*//' file
Sep 17 20:47:02 Fuel has High Warning
Sep 20 12:02:20 Genset Started
Sep 20 12:09:31 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 20 12:10:27 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Most Severe Level
Sep 20 12:10:28 Genset Stopped
Sep 23 09:19:03 Genset Not In Auto
Sep 23 09:19:04 All Faults Cleared
Sep 23 09:19:13 Genset in Auto
Sep 30 12:33:48 Genset Not In Auto
Sep 30 12:33:55 Genset Started
Sep 30 12:41:14 Engine Coolant Temperature - Data Valid but Above Normal Operational Range - Moderately Severe Level
Sep 30 12:41:56 Genset Stopped
Sep 30 12:41:58 All Faults Cleared
Oct  3 14:00:52 Genset Started
Oct  3 14:15:17 Genset Stopped
Oct  3 14:20:39 Genset in Auto
Oct 15 12:02:55 Genset Started
Oct 15 12:12:50 Genset Stopped

관련 정보