0

I want to remove comments reliably without removing newlines (\n) and any piece of essential Matlab code by Unix commands, not by extensions/gadgets/... existing for the target etc here. Test cases

  1. The symbol % elsewhere in the code, not indicating comment etc in sprintf('Masi % score').
  2. Ignore tricky ones etc sprintf('Masi % score'); % do not need to remove this comment because tricky
  3. ...

I think SED can work well. Some attempts

  • Attempt 1 faulty because removes newlines and fails test case 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
    
  • Attempt 2 faulty because includes only first line in output

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

Example inputFile.m as data file

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

OS: Debian 8.5 64 bit
Tools: SED, Python, Perl, ...

5
  • 1
    Maybe you could use matlab code? de.mathworks.com/matlabcentral/newsreader/view_thread/262710
    – rudimeier
    Commented Oct 5, 2016 at 11:56
  • 1
    This one is tricky: sprintf('Masi % score'); % Remove this comment. It should be something like sed -e "s/\(^.*'.*%.*'.*\)%.*$/\1/"
    – hschou
    Commented Oct 5, 2016 at 11:56
  • I am not trying to argue against your use case, but why do you want to strip out the comments? Also, consider stack overflow (healthy stack there under 'matlab' tag).
    – Jeter-work
    Commented Oct 5, 2016 at 12:12
  • @Xalorous I have bugs in my sources codes. I need to make much test cases all the time to maintain the integrity. I use binary search strategy i.e. I delete some parts of codes and try to find the reason why the bug(s) came in the first place. In complex systems, it takes a lot of time. Comments 2-3x the length of my codes often. To have a different view of codes can help. Commented Oct 5, 2016 at 12:15
  • Thank you. What about search and replace in vim? Or sed with regex to select any line that starts with a comment character. Both cases would probably take the whole line, including the newline, but maybe that would be acceptable? Also, are there any graphical IDE's that can hide comments? SO about folding comments. Also, it mentions notepad++ and visual studio. Yuck windows, I know, but there should be a graphical editor to let you do the same in Linux.
    – Jeter-work
    Commented Oct 5, 2016 at 12:28

2 Answers 2

1

Try this:

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

This removes line-ending comments, only if the comment text does not contain a single quote. It leaves the line's newline.

0

I found an answer. Set the color of comments the same as the background.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .