V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nyanyh
V2EX  ›  问与答

有偿求解一个从文本文件中搜索特定内容的正则表达式

  •  
  •   nyanyh · 2019-10-11 14:05:23 +08:00 · 1213 次点击
    这是一个创建于 1651 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个仿真程序,跑完生成一个约 20M 的 log 文件,想从里面找到特定格式的一行内容

    Class           =                        C
    Size            =            162x 162x 162
    Iterations      =                      200
    Time in seconds =                    23.68
    Total processes =                      900
    Compiled procs  =                      900
    Mop/s total     =                121032.40
    Mop/s/process   =                   134.48
    

    这段 log 里想获取**Time in seconds = **后面空格以后到结尾换行符之间的内容,正常情况下是一个小数(小数部分 0-5 位不定),但是如果仿真超时了这里会是一些未知文字(没超时过所以不知道是什么)。感觉用 grep 就直接能弄出来,但无奈是不会……

    9 条回复    2019-10-12 21:13:56 +08:00
    kamal
        1
    kamal  
       2019-10-11 14:11:48 +08:00   ❤️ 1
    nyanyh
        2
    nyanyh  
    OP
       2019-10-11 14:31:34 +08:00
    @kamal #1 可以,问题解决了,给个收款码吧😀
    kamal
        3
    kamal  
       2019-10-11 17:44:20 +08:00   ❤️ 1
    @nyanyh #2 哈哈哈,给我点个赞就够啦
    gIrl1990
        4
    gIrl1990  
       2019-10-11 18:12:15 +08:00
    https://blog.csdn.net/u012047933/article/details/38365541
    https://perldoc.perl.org/5.30.0/perlre.html#Lookaround-Assertions
    pcre2 pcre http://www.pcre.org/

    你的要求: 空格以后到结尾换行符之间的内容, 所以用 断言
    因为: lookbehind assertion is fixed length 所以没法直接提取后面的内容
    我这里用 xargs 来 trim 内容

    ```bash
    pcre2grep -o "(?<=^Time in seconds = ).+" test.txt | xargs -n 1 echo
    `
    gIrl1990
        5
    gIrl1990  
       2019-10-11 18:20:01 +08:00
    用 xargs 不太好,会略过空白输出,比如
    ```text
    Time in seconds = 23.68
    Time in seconds = end
    Time in seconds = (end,没有括号内字符)
    ```

    ```bash
    pcre2grep -o "(?<=^Time in seconds =).+" test.txt
    ```
    nyanyh
        6
    nyanyh  
    OP
       2019-10-12 12:15:30 +08:00
    @kamal #3 好吧,真心感谢大佬的帮助🙏
    nyanyh
        7
    nyanyh  
    OP
       2019-10-12 12:16:19 +08:00
    @gIrl1990 #4 是的,就是这个数量不定的空格不好处理,现在我在正则提取完之后用 awk 把值弄出来了
    param
        8
    param  
       2019-10-12 21:03:38 +08:00 via Android
    v2 有没有有偿求助专区?
    gIrl1990
        9
    gIrl1990  
       2019-10-12 21:13:56 +08:00
    @param 我觉得可以开一个 node
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5311 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:50 · PVG 16:50 · LAX 01:50 · JFK 04:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.