我有一个日志文件,需要从中提取特定的数据元素。
示例文本:
[22-12-20 21:16:04.521] FROM LIVE <$011B4B50:FleetCard_10:1,51.75,1,1,200001,Fleet No,5411,34319,TARJETA,,51.75,,3,0,1>
[22-12-20 21:16:04.553] auth accepted tag=9812120450668474 device=V2
[22-12-20 21:16:40.185] FROM LIVE <02:PAYDONE=0000022851>
[22-12-20 21:17:20.677] TO LIVE <$011B4910:FleetCard_1:9812120450669349>
[22-12-20 21:17:21.270] FROM LIVE <$011B4910:FleetCard_10:1,49.48,1,1,200001,Fleet No,5237,34320,TARJETA,,49.48,,2,0,1>
[22-12-20 21:17:21.333] auth accepted tag=9812120450669349 device=V1
[22-12-20 21:18:44.345] FROM LIVE <02:PAYDONE=0000022852>
[22-12-20 21:19:16.399] FROM LIVE <03:PAYDONE=0000022853>
[22-12-20 21:20:18.292] TO LIVE <$011B5150:FleetCard_1:9812120450669482>
[22-12-20 21:20:19.073] FROM LIVE <$011B5150:FleetCard_10:1,51.75,1,1,200001,Fleet No,2001,34321,TARJETA,,51.75,,3,0,1>
[22-12-20 21:20:19.167] auth accepted tag=9812120450669482 device=V1
[22-12-20 21:21:53.536] TO LIVE <$011B4B50:FleetCard_1:9812120450668854>
[22-12-20 21:21:54.286] FROM LIVE <$011B4B50:FleetCard_10:1,51.75,1,1,200001,Fleet No,5418,34322,TARJETA,,51.75,,3,0,1>
[22-12-20 21:21:54.301] auth accepted tag=9812120450668854 device=V2
[22-12-20 21:25:11.284] FROM LIVE <02:PAYDONE=0000022854>
[22-12-20 21:25:20.141] FROM LIVE <04:PAYDONE=0000022855>
我想提取如下内容
Fleet No : 5411 tag :9812120450668474
Fleet No: 5237 tag:9812120450669349
Fleet No : 2001 tag:9812120450669482
如何在此实现这一目标?
答案1
- Ctrl+H
- 找什么:
.+?(Fleet No),(\d+).+?\R.+?(tag)=(\d+).+?(\R).+?
- 用。。。来代替:
$1 : $2 $3 : $4$5
- 打钩 相符
- 打钩 环绕
- 选择 正则表达式
- 打钩
. matches newline
- Replace all
解释:
.+? # 1 or more any character, not greedy
(Fleet No) # group 1, literally
, # comma
(\d+) # group 2, 1 or more digits
.+? # 1 or more any character, not greedy
\R # any kind of linebreak
.+? # 1 or more any character, not greedy
(tag) # group 3, literally
= # equal sign
(\d+) # group 4, 1 or more digits
.+? # 1 or more any character, not greedy
(\R) # group 5, any kind of linebreak
.+? # 1 or more any character, not greedy
截图(之前):
截图(之后):
您必须手动删除最后两行
答案2
如果我要从大文件中提取数据,我通常会使用 TextPad。
- 我把光标放在正在处理的行的开头。
- 我开始录制可以多次播放的宏。
- 我录制宏(带或不带 REGEX 表达式)并将行缩减为我需要的数据,以便只有该数据留在该行上。这通常会将我的光标移到我需要的数据的开头,按下 Shift Home,然后按下 Delete 键。然后我再次搜索,将光标移到数据的末尾,按下 Shift End,然后按下 Delete 键。现在只有我的数据在该行上。
- 此后,我按下 Home 键将光标移动到当前行的开头。
- I 向下箭头 1 行
- 我停止录音
它会询问您是否要播放到文件末尾或播放一次。
然后我会问你是否要保存它。
然后我按下 Control R 来播放每一行要转换的内容。