SED: Matching on 2 patterns on the same line

前端 未结 3 484
长发绾君心
长发绾君心 2021-01-21 10:19

Hi I want to delete a line using sed if it matches 2 regular expressions in the same line. EG the line starts with /* and end with */ (comment). The following script will do m

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 10:46

    this strips out multiline comments as well

    eg

    # cat file
    blah blah /* comment */
    words1
    words2
    /* multiline
       comments
    /*
    end
    
    $ awk -vRS='*/'  '{ gsub(/\/\*.*/,""); }1' file
    blah blah
    
    words1
    words2
    

    you can add another filter to sed 's|\/\/.*||' to filter out // comments as well

提交回复
热议问题