2011-11-09 88 views
0

我在目錄中有很多c文件。每個文件將與多行註釋開始像下面,並開始與包括像刪除所有* c文件中的第一個多行註釋

#include<stdio.h>. 

    // This is a c File 

    // This File does sampling the frequency 

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

#include<stdio.h> 

我需要在所有的.c文件開始刪除所有的註釋行,如果它們存在。有些文件不會有這些註釋行。在這種情況下,它不應該做任何事情。這應該只刪除第一個多行註釋。隨後的多行註釋應該保持原樣。

如何在perl或shell中執行此操作?

在此先感謝

+1

頭行不提問內容匹配嗎? – hfs

+0

重要缺失信息:第一個#include不在您的原始文件中,我認爲這是編輯器錯誤?什麼規則適用於「第一評論」?任何'#include'語句之前的註釋? – TLP

+0

你會如何判斷「它不在」的情況?看起來你總是要看看是否有'#include'來知道你是否想要刪除一個多行註釋。 *誰曾經想過放入* BEFORE *文件。 – Axeman

回答

0
#!/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*\*/?); 
+0

我想只爲第一個多行註釋做到這一點。接下來的多個命令應該是prsent,因爲它是 – Raj

+0

@Raj:請參閱上面編輯的解決方案。 – JRFerguson

+0

爲了安全起見,請在不同目錄中創建新文件。 – Itamar

1

如果您確信所有文件開始的包括,你可以在導入前刪除所有行:

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

如何從shell腳本執行此命令? – Raj

+0

我想在shell腳本的循環中執行此操作。 – Raj

+0

'-n'命令行選項*是*循環。它在腳本週圍使用了一個隱含的'while(<>)'循環。 – TLP

0

我有一個非常簡單的解決方案如果#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> 
相關問題