Matlabコードからほとんどのコメントを確実に削除する方法は?

Matlabコードからほとんどのコメントを確実に削除する方法は?

\n既存の拡張/ガジェット/...を使用する代わりに、Unixコマンド(改行を削除)と基本的なMatlabコードを介して改行なしでコメントを確実に削除したいと思います。ここ。テストケース

  1. %sprintf('Masi % score')表現などのコメント以外のコードの他の場所にある記号
  2. 難しい質問などは無視してください。sprintf('Masi % score'); % do not need to remove this comment because tricky
  3. ...

私はSEDが非常にうまく機能できると思います。いくつかの試み

  • 改行が削除され、テストケース1が失敗するため、試行1エラー

    # http://stackoverflow.com/a/3350246/54964
    sed -e 's/%.*$//' -e '/^$/d' inputFile.m
    
    ## Output 
    function blalala(var2);
    var=1;      
    hello=2; 
    assert(indexPositionEnd >= indexPositionStart, 'indexEnd bigger/equal than indexStart');
    index=index+1
    pause(1); 
    sprintf('Masi 
    end
    
  • 最初の行だけが出力に含まれているため、試行2エラーが発生しました。

    # http://stackoverflow.com/a/1252191/54964
    sed ':a;N;$!ba;s/%.*$/ /g' inputFile.m
    
    ## Output  
    function blalala(var2);
    

inputFile.mデータファイルの例

function blalala(var2);
%% synapse
% describe here pla la
%
var=1;      
% 
hello=2; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      Statistics and Monitoring
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

assert(indexPositionEnd >= indexPositionStart, 'indexEnd bigger/equal than indexStart');

index=index+1

%% Situation monitoring at the end
pause(1); % waitforbuttonpress pause is not sufficient

% http://stackoverflow.com/a/111322....

sprintf('Masi % score').
sprintf('Masi % score'); % do not need to remove this comment because tricky

end

オペレーティングシステム:Debian 8.5 64ビット
ツール:SED、Python、Perl ...

ベストアンサー1

この試み:

sed 's/[[:blank:]]*%[^'\'']*$//' 

コメントテキストに一重引用符が含まれていない場合にのみ、行末のコメントは削除されます。その行に改行文字が残ります。

おすすめ記事