Regex删除块注释,同时删除*选择器

Regex删除块注释,同时删除*选择器,regex,linux,bash,unix,sed,Regex,Linux,Bash,Unix,Sed,我正在尝试使用bash从.css文件中删除所有块注释。对于sed命令,我有以下正则表达式: sed -r '/\/(\*)*(\s)?(\w)*|(\*)(\s)?(\w)*/d' 这可以很好地去除块注释,例如: /** * This is a comment */ /* this is another comment */ 但它也删除了*选择器的实例 * { font-size: 10px; ...etc } 如何修改当前的正则表达式以考虑这个特殊情况?还是我

我正在尝试使用bash从.css文件中删除所有块注释。对于sed命令,我有以下正则表达式:

sed -r '/\/(\*)*(\s)?(\w)*|(\*)(\s)?(\w)*/d'
这可以很好地去除块注释,例如:

/**
* This is a comment
*/

/* this is another comment */
但它也删除了*选择器的实例

* { 
     font-size: 10px;
     ...etc
  }
如何修改当前的正则表达式以考虑这个特殊情况?还是我需要重写我的正则表达式

编辑: 为了澄清,我将遇到的评论类型如下:

/**
* This is a comment
*/

/* This is another comment */

/* This is also 
valid
*/

/** "*/" as is { this } */

以下
sed
命令应适用于您:

sed '/\/\*\**/{:a;/\*\//d;N;ba}' file.css
该命令搜索模式
/**
/*
,如果找到该模式,则执行大括号
{cmd1;cmd2;…}
之间的命令块。在该块中,我首先定义一个标签
:a
。在下一个命令中,我检查模式缓冲区是否包含关闭的
*/
。如果找到了,我将删除模式缓冲区并开始下一个循环。否则,我通过
N
将下一行输入附加到pattern缓冲区,并通过
ba
转到标签
a

file.css:

/**
* This is a comment
*/
a {
    font-size: 10px;
}

/* This is another comment */
li {
    font-size: 12px;
}

/* This is also 
valid
*/
* {
    color:white;
}

/** "*/" as is { this } */
table {
    color:blue;
}
输出:

a {
    font-size: 10px;
}

li {
    font-size: 12px;
}

* {
    color:white;
}

table {
    color:blue;
}

以下
sed
命令应适用于您:

sed '/\/\*\**/{:a;/\*\//d;N;ba}' file.css
该命令搜索模式
/**
/*
,如果找到该模式,则执行大括号
{cmd1;cmd2;…}
之间的命令块。在该块中,我首先定义一个标签
:a
。在下一个命令中,我检查模式缓冲区是否包含关闭的
*/
。如果找到了,我将删除模式缓冲区并开始下一个循环。否则,我通过
N
将下一行输入附加到pattern缓冲区,并通过
ba
转到标签
a

file.css:

/**
* This is a comment
*/
a {
    font-size: 10px;
}

/* This is another comment */
li {
    font-size: 12px;
}

/* This is also 
valid
*/
* {
    color:white;
}

/** "*/" as is { this } */
table {
    color:blue;
}
输出:

a {
    font-size: 10px;
}

li {
    font-size: 12px;
}

* {
    color:white;
}

table {
    color:blue;
}
带着awk

awk '!/\/?\*/||/{/'  file
解释
表示没有
/
包含正则表达式
\/?
0或1 fwd斜杠需要转义,因为
/
是正则表达式的delim
\*
Literal
*

作为一个整体,匹配不以
/*

||
或用于将语句链接在一起的命令,如果第一个命令失败,它将短路并不执行第二个命令(尽管与此无关)

如果该行包含一个开括号,则执行命令块

没有命令块,默认操作是打印

因此,作为一个整体,命令
“!/^\/\*/||/{/'
表示如果该行不包含
/*
*
或包含
{
则打印该行


如果注释包含
{

awk '/\/\*/{x=1}!x;/\*\//{x=0}' file
解释 这一个检查行是否有
/*
,并将
x
设置为1。
!x
表示在x为0或null时打印该行。它打印时没有操作块。 遇到
*/
时,将
x
设置回0


输出 不考虑被引用的内容,比如
/*“*/”你好,我还在评论


什么时候破
命令

awk '!/\/?\*/||/{/'
awk '/\/\*/{x=1}!x;/\*\//{x=0}'
文件

/*
I am comment that will print
*/
/*
I wont print "*/"
I will print as the "*/" is seen as the end of the comment
This line prints
*/ ## This line will also print at x has not been set back to one

命令

awk '!/\/?\*/||/{/'
awk '/\/\*/{x=1}!x;/\*\//{x=0}'
文件

/*
I am comment that will print
*/
/*
I wont print "*/"
I will print as the "*/" is seen as the end of the comment
This line prints
*/ ## This line will also print at x has not been set back to one
带着awk

awk '!/\/?\*/||/{/'  file
解释
表示不
/
包含正则表达式
\/?
0或1 fwd斜杠需要转义,因为
/
是正则表达式的delim
\*
Literal
*

作为一个整体,匹配不以
/*

||
或用于将语句链接在一起的命令,如果第一个命令失败,它将短路并不执行第二个命令(尽管与此无关)

如果该行包含一个开括号,则执行命令块

没有命令块,默认操作是打印

因此,作为一个整体,命令
'!/^\/?\*/|*/{/'
意味着如果该行不包含
/*
*
或它包含
{
则打印该行


如果注释包含
{

awk '/\/\*/{x=1}!x;/\*\//{x=0}' file
解释 这一个检查行是否有
/*
,并将
x
设置为1。
!x
表示在x为0或null时打印该行。它打印时没有操作块。 遇到
*/
时,将
x
设置回0


输出 不考虑被引用的内容,比如
/*“*/”你好,我还在评论


什么时候破
命令

awk '!/\/?\*/||/{/'
awk '/\/\*/{x=1}!x;/\*\//{x=0}'
文件

/*
I am comment that will print
*/
/*
I wont print "*/"
I will print as the "*/" is seen as the end of the comment
This line prints
*/ ## This line will also print at x has not been set back to one

命令

awk '!/\/?\*/||/{/'
awk '/\/\*/{x=1}!x;/\*\//{x=0}'
文件

/*
I am comment that will print
*/
/*
I wont print "*/"
I will print as the "*/" is seen as the end of the comment
This line prints
*/ ## This line will also print at x has not been set back to one

下面的代码适用于您提供的所有场景

sed ':1;/^ *\/\*/{:2;/\*\/$/{d;b 1;};N;b 2;}' filename

下面的代码适用于您提供的所有场景

sed ':1;/^ *\/\*/{:2;/\*\/$/{d;b 1;};N;b 2;}' filename

它必须与sed一起使用吗?最好是,但我愿意接受建议,我愿意阅读awk或任何其他
/**“*/”{this}*/
:即使
*/
用双引号括起来,它始终被视为一个结束序列。因此注释部分是
/**“*/
是否必须使用sed?最好是,但我愿意接受建议,我愿意阅读awk或其他任何
/**“*/”{this}*/
:即使
*/
包含在双引号中,也总是被视为结束序列。因此注释部分是
/**“*/
离开
/*这是另一个注释*/
行,需要删除
*
sed'/\/\*/{:a;/\*\//d;N;ba}”“
比我快,它保留了*选择器,但也保留了单行注释,如/*comment*/它没有保留单行注释,它只保留以
/*
开头的注释,而不是od
/**
。我是有意这样做的,因为你在问题中指出了这一点。编辑it@hek2mgl正则表达式的:a;N;ba部分在做什么?不需要详细的解释,如果你能在网站上给我一个合适的指南
\**
使
*
成为可选的,你应该删除留下
/*的量词
\*
,这是另一个注释*/
行,需要删除
*