从所有 *c 文件中删除第一个多行注释

发布于 2024-12-15 02:45:58 字数 397 浏览 0 评论 0原文

我的目录中有很多c 文件。每个文件将以多行注释开头,如下所示,并以 include 开头,就像

#include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>

我需要删除所有 .c 文件开头的所有注释行(如果存在)。有些文件不会有这些注释行。在这种情况下,它不应该执行任何操作。这应该只删除第一个多行注释。后续的多行注释应该保持原样。

如何在 Perl 或 shell 中执行此操作?

提前致谢

I have lot of c files in a directory. Each files will start with multi line comment like below and starts with include like

#include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>

I need to remove all the comments line at the start of all the .c files if they are present. Some files will not have these comment lines. In that case, it should not do anything. This should remove only the first multi line comments. Subsequent multi line comments should be as it is.

How to do this in perl or shell?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉城メ夜风 2024-12-22 02:45:58

如果您确定所有文件都以包含开头,则可以在导入之前删除所有行:

perl -i -ne 'print if /^#include/..0' *.c

If you are sure that all files start with an include, you could remove all lines before the import:

perl -i -ne 'print if /^#include/..0' *.c
思念绕指尖 2024-12-22 02:45:58
#!/usr/bin/env perl
use strict;
use warnings;
my $skipping = 0;
while (<>) {
    m{^\s*//}           and next;
    m{^\s*/\*.*\*/\s*$} and next;
    if ( m{^\s*/\*} ) {
        $skipping = 1;
        next;
    }
    if ( m{^\s*\*/} ) {
        $skipping = 0;
        next;
    }
    next if $skipping;
    print;
}

...或者:

#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
    next if      m{^\s*//};
    next if      m{^\s*/\*.*\*/\s*$};
    next while ( m{^\s*/\*}..m{^\s*\*/} );
    print;
}

...并且如果您只想对第一个多行注释执行此操作,请将范围的匹配分隔符从大括号更改为“?”字符,读起来像这样:

next while ( m?^\s*/\*?..m?^\s*\*/? );
#!/usr/bin/env perl
use strict;
use warnings;
my $skipping = 0;
while (<>) {
    m{^\s*//}           and next;
    m{^\s*/\*.*\*/\s*$} and next;
    if ( m{^\s*/\*} ) {
        $skipping = 1;
        next;
    }
    if ( m{^\s*\*/} ) {
        $skipping = 0;
        next;
    }
    next if $skipping;
    print;
}

...or alternatively:

#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
    next if      m{^\s*//};
    next if      m{^\s*/\*.*\*/\s*$};
    next while ( m{^\s*/\*}..m{^\s*\*/} );
    print;
}

...AND if you only want to do this for the first multi-line comment, change the match delimiters of the range from curly braces to the "?" character, to read like this:

next while ( m?^\s*/\*?..m?^\s*\*/? );
疯了 2024-12-22 02:45:58

如果 #include 可以被视为一个阻止者,我有一个非常简单的解决方案:

use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;

*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;

while ( <> ) { 
    if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
        substr( $_, $LAST_MATCH_START[0] ) = '';
        print;
        last;
    }
    print;
    last if m/^\s*#include\b/m;
}
$RS = $old_rs;
print while <>;

__DATA__
##include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* A mline comment not multi */

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>

请注意,我必须更改初始的 #include 以便脚本正常工作。对我来说,如果我想要多行注释,最简单的解决方案是将 '*/' 作为我的记录分隔符,而不是在各行上进行大量切换。

但是前瞻需要缓冲并提出了一个更混乱的解决方案:

use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;

*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;

my ( $rin, $rie );
my $mline = '';
my $buffer;

while ( <> ) { 
    if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
        my $split = $LAST_MATCH_START[0];
        print substr( $_, 0, $split );
        $mline = substr( $_, $split );
        last;
    }
    print;
}
$RS = $old_rs;
while ( <> ) { 
    $buffer .= $_;
    if ( /^\s*#include\b/ ) { 
        $mline = '';
        last;
    }
}
print $mline, $buffer;
print while <>;

__DATA__
#include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* A mutli-line comment not on multiple lines */

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>

I had a pretty easy solution, if #include could be considered a stopper:

use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;

*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;

while ( <> ) { 
    if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
        substr( $_, $LAST_MATCH_START[0] ) = '';
        print;
        last;
    }
    print;
    last if m/^\s*#include\b/m;
}
$RS = $old_rs;
print while <>;

__DATA__
##include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* A mline comment not multi */

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>

Notice that I had to change the initial #include so that the script works. It seemed simple to me that if I wanted multi-line comments, the simplest solution was to make '*/' my record separator, instead of doing a lot of switching on the individual lines.

But the lookahead, requires buffering and made a messier solution:

use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;

*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;

my ( $rin, $rie );
my $mline = '';
my $buffer;

while ( <> ) { 
    if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
        my $split = $LAST_MATCH_START[0];
        print substr( $_, 0, $split );
        $mline = substr( $_, $split );
        last;
    }
    print;
}
$RS = $old_rs;
while ( <> ) { 
    $buffer .= $_;
    if ( /^\s*#include\b/ ) { 
        $mline = '';
        last;
    }
}
print $mline, $buffer;
print while <>;

__DATA__
#include<stdio.h>. 

   // This is a c File

   // This File does sampling the frequency

   /* A mutli-line comment not on multiple lines */

   /* This uses the logic ...
   .....
   .....
   */

#include<stdio.h>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文