我有一个充满此类型日志的日志文件:
2020-02-04 04:00:31,503 [z4y6480f-214b-4253-9223-n02542f706ac] [INFO] [ServiceType] [ObjectType] - Information about the log
我想使用正则表达式模式来检索时间,方括号中的最后一个文本(例如[ObjectType])和连字符后的信息消息。
输入示例:
2020-02-04 04:00:33,435 [z4y6480f-214b-4253-9223-n02542f706ac] [INFO] [ServiceTypeJohn] [ObjectTypeJohn] - Information about the John log
2020-02-04 06:50:34,465 [z4y6480f-214b-4253-9223-n02542f706ac] [INFO] [ServiceTypeBob] [ObjectTypeBob] - Information about the Bob log
2020-02-04 07:20:34,677 [z4y6480f-214b-4253-9223-n02542f706ac] [INFO] [ServiceTypeSam] [ObjectTypeSam] - Information about the Sam log
所需输出:
04:00:33,435 [ObjectTypeJohn] - Information about the John log
06:50:34,465 [ObjectTypeBob] - Information about the Bob log
07:20:34,677 [ObjectTypeSam] - Information about the Sam log
到目前为止,我已经尝试过了但是没有成功:
(Get-Content Output.txt) -replace '^(\d\d:\d\d:\d\d).*(\[.*?\] - .*?)$','$1;$2'
希望对此有所帮助,谢谢。
最佳答案
您可以使用
(Get-Content Output.txt) -replace '^\S+\s+(\S+).*(\[[^][]*])\s*(-.*)', '$1 $2 $3'
见.NET regex demo
详细信息
^
-字符串\S+
-除空格\s+
-1+空格(\S+)
-组1:1个以上除空格的字符.*
-除换行符外的任何0+个字符,尽可能多的(\[[^][]*])
-组2:[
,除[
和]
之外的0+个字符,然后是]
char \s*
-1+空格(-.*)
-组3:-
和字符串的其余部分。 演示结果:
关于regex - 使用Regex模式分析日志文件中的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60637806/