【问题标题】:How to isolate timestamp and expression from log file using regex如何使用正则表达式从日志文件中隔离时间戳和表达式
【发布时间】:2021-08-27 04:02:32
【问题描述】:

我有一个看起来像这样的日志文件:

04:26:24.664149 [PHY1] [I] [4198] PUCCH: cc=0; rnti=0x46, f=1a, n_pucch=12, dmrs_corr=0.995, snr=13.2 dB, corr=0.974, ack=1, ta=0.1 us

04:26:24.665067 [PHY0] [D] [4199] Worker 0 正在运行

04:26:24.665166 [PHY0] [D] [4199] 发送到电台

04:26:24.666220 [PHY1] [I] [4200] PUCCH: cc=0; rnti=0x46, f=1, n_pucch=0, dmrs_corr=0.270, snr=-4.3 dB, corr=0.000, sr=no, ta=-9.0 us

04:26:24.666288 [PHY1] [D] [4200] 发送到电台

04:26:24.667305 [PHY0] [I] [4201] PUCCH: cc=0; rnti=0x46, f=2, n_pucch=0, dmrs_corr=0.989, snr=15.4 dB, corr=0.998, cqi=15 (cc=0), ta=0.2 us

04:26:24.667338 [MAC] [D] [4201] ra_tbs=72/144, tbs_bytes=15, tbs=144, mcs=2

我想隔离存在 snr={value} 条目的行,并复制与该条目关联的时间戳。我已将要使用正则表达式提取的示例部分放入 bold

我尝试了许多不同的正则表达式来尝试从我的日志文件中提取这两位信息(在它们存在的行上)。重要的是要注意 snr 值可以是正值或负值,并且可以在 -999.9 dB 到 999.9 dB 之间变化。时间戳出现在日志文件的每一行。

我的预期输出示例如下:04:26:24.664149 snr=13.2

任何帮助将不胜感激!

【问题讨论】:

    标签: python regex parsing python-re


    【解决方案1】:

    这是使用re.findall的一种方法:

    inp = """04:26:24.664149 [PHY1   ] [I] [ 4198] PUCCH: cc=0; rnti=0x46, f=1a, n_pucch=12, dmrs_corr=0.995, **snr=13.2** dB, corr=0.974, ack=1, ta=0.1 us
    04:26:24.665067 [PHY0   ] [D] [ 4199] Worker 0 running
    04:26:24.665166 [PHY0   ] [D] [ 4199] Sending to radio
    04:26:24.666220 [PHY1   ] [I] [ 4200] PUCCH: cc=0; rnti=0x46, f=1, n_pucch=0, dmrs_corr=0.270, **snr=-4.3** dB, corr=0.000, sr=no, ta=-9.0 us
    04:26:24.666288 [PHY1   ] [D] [ 4200] Sending to radio"""
    
    matches = re.findall("(\d{2}:\d{2}:\d{2}\.\d{6})[^\r\n]*(snr=-?\d+(?:\.\d+)?)", inp)
    print(matches)
    

    打印出来:

    [('04:26:24.664149', 'snr=13.2'), ('04:26:24.666220', 'snr=-4.3')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2016-08-03
      • 2014-12-15
      • 1970-01-01
      相关资源
      最近更新 更多