我有Home Assistant日誌文件,我需要通過bash中的腳本從它提取特定的信息。使用正則表達式從日誌文件中提取字符串
日誌是這樣的:
2017-09-26 20:54:09 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.player1=playing; media_duration=6054, is_volume_muted=False, media_season=-1, media_episode=-1, media_series_title=, media_content_type=movie, media_album_name=, friendly_name=player1, media_title=My movie, supported_features=55231, entity_picture=/api/media_player_proxy/media_player.player1?token=5ce13d0e4d8ed86b3073a795a4bd82627b387cf2b6c5dfdc8c64ab9b62ec7d61&cache=f4a48, media_content_id=unknown=tt0266543, volume_level=0.91 @ 2017-09-26T20:44:37.080005+02:00>, old_state=<state media_player.player1=playing; media_duration=6054, is_volume_muted=False, media_season=-1, media_episode=-1, media_series_title=, media_content_type=movie, media_album_name=, friendly_name=player1, media_title=My movie, supported_features=55231, entity_picture=/api/media_player_proxy/media_player.player1?token=5ce13d0e4d8ed86b3073a795a4bd82627b387cf2b6c5dfdc8c64ab9b62ec7d61&cache=23108, media_content_id=unknown=tt0266543, volume_level=0.91 @ 2017-09-26T20:44:37.080005+02:00>, entity_id=media_player.player1>
2017-09-26 21:30:22 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.tv_player2=playing; is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:22.660153+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, old_state=<state media_player.tv_player2=playing; supported_features=21437, is_volume_muted=False, media_position_updated_at=2017-09-26T21:30:22.557344+02:00, app_id=233637DE, friendly_name=player2, media_content_id=AcADAFAD, app_name=YouTube, media_position=0, volume_level=1.0 @ 2017-09-26T21:30:21.848088+02:00>, entity_id=media_player.tv_player2>
2017-09-26 21:30:22 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.tv_player2=playing; is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:22.695325+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, old_state=<state media_player.tv_player2=playing; is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:22.660153+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, entity_id=media_player.tv_player2>
2017-09-26 21:30:29 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.tv_player2=playing; media_duration=1789.213605442177, is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:29.115053+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, old_state=<state media_player.tv_player2=playing; is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:22.695325+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, entity_id=media_player.tv_player2>
2017-09-26 21:30:29 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.tv_player2=playing; media_duration=1789.213605442177, is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0.733, supported_features=21437, media_position_updated_at=2017-09-26T21:30:29.843295+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, old_state=<state media_player.tv_player2=playing; media_duration=1789.213605442177, is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0, supported_features=21437, media_position_updated_at=2017-09-26T21:30:29.115053+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, entity_id=media_player.tv_player2>
2017-09-26 21:31:34 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.tv_player2=playing; media_duration=1789.213605442177, is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=65.681, supported_features=21437, media_position_updated_at=2017-09-26T21:31:34.823855+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, old_state=<state media_player.tv_player2=playing; media_duration=1789.213605442177, is_volume_muted=False, app_id=233637DE, app_name=YouTube, volume_level=1.0, friendly_name=player2, media_position=0.733, supported_features=21437, media_position_updated_at=2017-09-26T21:30:29.843295+02:00, entity_picture=/api/media_player_proxy/media_player.tv_player2?token=f03173d412f3c7396762605a4f347b83f651d4c0f23935c8b1fccd9305698ffe&cache=88a6b, media_content_id=AcADAFAD, media_title=YT test movie @ 2017-09-26T21:30:21.848088+02:00>, entity_id=media_player.tv_player2>
2017-09-26 22:45:58 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.player1=playing; media_duration=0, is_volume_muted=False, media_album_name=, media_content_type=music, friendly_name=player1, volume_level=0.75, supported_features=55231, media_title=My radio R1 @ 2017-09-26T22:44:57.441924+02:00>, old_state=<state media_player.player1=playing; media_duration=0, is_volume_muted=False, media_album_name=, media_content_type=music, friendly_name=player1, volume_level=0.75, supported_features=55231, media_title= @ 2017-09-26T22:44:57.441924+02:00>, entity_id=media_player.player1>
2017-09-26 22:46:09 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.player1=playing; media_duration=0, is_volume_muted=False, media_album_name=, media_content_type=music, friendly_name=player1, volume_level=0.75, supported_features=55231, media_title= @ 2017-09-26T22:44:57.441924+02:00>, old_state=<state media_player.player1=playing; media_duration=0, is_volume_muted=False, media_album_name=, media_content_type=music, friendly_name=player1, volume_level=0.75, supported_features=55231, media_title=My radio R1 @ 2017-09-26T22:44:57.441924+02:00>, entity_id=media_player.player1>
我需要大約腳本信息的提取:時間(先發生),FRIENDLY_NAME和media_title沒有duplicites在media_title。
像這樣在輸出文件:
2017-09-26 20:54:09, player1, My movie
2017-09-26 21:30:22, player2, YT test movie
2017-09-26 22:45:58, player1, My radio R1
我試過的grep,sed和無propper結果。 謝謝
比賽'([ - \ d:] +) INFO。* friendly_name =([\ w] +)。* media_title =([\ w] +)。*'並用'\ 1,\ 2,\ 3'替換https://regex101.com/r/sM1AOj/1 – CAustin
請分享您嘗試的內容,以便我們幫助您確定問題所在。 – ghoti