Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
出于差异目的,我需要删除一段代码的注释部分。
我使用 grep 和 sed 删除所有以 #、// 和 ; 开头的行,以及由 /* 和 */ 包围的多行。
一个问题是该规则还删除了以 #include 和 #!/bin/whatever 开头的行,它们是有效的代码语句。
有没有办法保留包含 #include 和 #! 的行 在开头但使用 grep 或 bash 删除以 # 开头的其余行。
提前感谢您的回复。
我认为最简单的方法是用这样的管道传输你原来的 grep 命令egrep -v:
egrep -v
grep "your-pattern" *.c | egrep -v "^\s*#(!|include|define)\b"
使用\转义,换句话说,如果你想保留#使用它,就像\#. 但是您能否更具体地了解编程语言,以便为您提供准确的答案
\
#
\#
egrep "^ *([^ #]|#!|#include|$)"