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

sed 多行贪婪匹配

  •  
  •   cryovz · 2017-04-17 20:08:59 +08:00 · 1286 次点击
    这是一个创建于 2590 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文件

    First line
    /* Comments
    Comments
    */
    */
    asdf
    asdf
    asdf
    
    as/* Comments
    asdf
    */
    

    我想使用 sed 跨行匹配注释,匹配后的效果如下。

    First line
    asdf
    asdf
    asdf
    
    as/* Comments
    asdf
    */
    

    我写的一个语句,但是不能贪婪匹配

    sed -i '/^[a-z0-9A-Z_ \t]*\/\*/,/.*\*\//d' filename
    

    效果如下,仍有一个*/没有匹配,但是又不能单独匹配删除*/,因为会删除其他的*/

    First line
    */
    asdf
    asdf
    asdf
    
    as/* Comments
    asdf
    */
    
    请问如何在匹配多行的情况下,进行贪婪匹配,
    
    2 条回复    2017-04-17 22:37:28 +08:00
    neilp
        1
    neilp  
       2017-04-17 20:50:08 +08:00
    如果我没记错的话, sed 只能处理单行.
    DiamondbacK
        2
    DiamondbacK  
       2017-04-17 22:37:28 +08:00
    没看懂你的匹配标准。
    实例与陈述不一致。
    你用的方法,效果也不是你所说的那样。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3054 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.