SED: Matching on 2 patterns on the same line

前端 未结 3 482
长发绾君心
长发绾君心 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:51

    Try

    sed '/^\/\*.*\*\/$/ d' filename

    The key here is that you can sorta combine two regex patterns into one, simply by connecting them with .*, which matches "any number of any character". Of course, this enforces an ordering between the two. The first pattern ^\/\* must occur before the second one \*\/$ for this particular pattern to match.

    Also, since * has special meaning in regex, be sure to escape your astrices, just as you have to escape your slashes.

提交回复
热议问题