我想可靠地删除注释,而不\n
通过 Unix 命令删除换行符 ( ) 和任何基本的 Matlab 代码,而不是通过目标等现有的扩展/小工具/...这里。测试用例
%
代码中其他地方的符号,不表示 中的注释等sprintf('Masi % score')
。- 忽略棘手的问题等
sprintf('Masi % score'); % do not need to remove this comment because tricky
- ...
我认为 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:]]*%[^'\'']*$//'
仅当注释文本不包含单引号时,这才会删除行结束注释。它留下了该行的换行符。
答案2
我找到了一个回答。将注释的颜色设置为与背景相同。