问题描述
我想使用sed删除一行,如果它与同一行中的2个正则表达式匹配. EG该行以/*开头,以*/结束(注释).以下脚本将完成大部分操作.sed -e'/^/*/d'-e'/*/$/d'文件名该脚本将删除所有以*开头和以*/结尾的行.我希望它仅在满足两个条件而不是一个条件时才删除该行.
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 most of that.sed -e '/^/*/ d' -e '/*/$/ d' filenameThis script will remove all lines that start with * and end with */. I want it to remove the line only if is meets both criteria not one.
推荐答案
尝试
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.
这篇关于SED:在同一行上匹配2种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!