Linux 从文本文件中删除注释行
我有一个文本文件,其中包含以下文本Linux 从文本文件中删除注释行,linux,shell,unix,command,Linux,Shell,Unix,Command,我有一个文本文件,其中包含以下文本 /* a comment line in a C program */ printf("It is /* NOT a comment line */\n"); x = 5; /* This is an assignment, not a comment line */ [TAB][SPACE] /* another empty comment line here */ /* another weird line, but not a comment l
/* a comment line in a C program */
printf("It is /* NOT a comment line */\n");
x = 5; /* This is an assignment, not a comment line */
[TAB][SPACE] /* another empty comment line here */
/* another weird line, but not a comment line */ y = 0;
我只想使用linux命令删除以/*
开头,以*/
结尾的行
为此,我编写了以下代码
egrep "^/*.+*/$" small.txt
我将文本保存在我的small.txt
文件中
但它输出所有只以*/
结尾的行
输出如下所示
/* a comment line in a C program */
x = 5; /* This is an assignment, not a comment line */
[TAB][SPACE] /* another empty comment line here */
我期望的输出是
/* a comment line in a C program */
您可以使用此
sed
删除注释行:
sed '\~^/\*.*\*/$~d' file.c
或者使用grep
:
grep -v '^/\*.*\*/$' file.c
grep '^/\*.*\*/$' file.c
要仅打印匹配行,请执行以下操作:
sed '\~^/\*.*\*/$~!d' file.c
或者使用grep
:
grep -v '^/\*.*\*/$' file.c
grep '^/\*.*\*/$' file.c
首先,我们将删除空格和制表符,如下所示
sed "s/^[ \t]*//" -i small.txt
这将用删除前导空格和制表符的文本替换small.txt
我们将运行以下命令来提取注释
grep '^/\*.*\*/$' small.txt
那跨过几行的注释呢?先删除间距和制表符,然后替换文件。???替换文件?你这是什么意思?