V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
cryovz
V2EX  ›  问与答

sed 多行贪婪匹配

  •  
  •   cryovz · Apr 17, 2017 · 1622 views
    This topic created in 3296 days ago, the information mentioned may be changed or developed.

    原文件

    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 replies    2017-04-17 22:37:28 +08:00
    neilp
        1
    neilp  
       Apr 17, 2017
    如果我没记错的话, sed 只能处理单行.
    DiamondbacK
        2
    DiamondbacK  
       Apr 17, 2017
    没看懂你的匹配标准。
    实例与陈述不一致。
    你用的方法,效果也不是你所说的那样。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6080 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 02:35 · PVG 10:35 · LAX 19:35 · JFK 22:35
    ♥ Do have faith in what you're doing.