MATLAB: Transfer lines in a text file to a new text file

text file

I need to transfer lines between two text files into a new text file. The text file in which I need to match lines is called m21rmmorphU.txt, and I need to match the lines belonging in the text file,virgoclusterrm.txt, that also belong in m21rmmorphU.txt, into a new text file.
So, I want to extract the lines that match in m21rmmorphU.txt from virgoclusterrm.txt into a new text file and remove the matched lines from m21rmmorphU.txt.
Files are attached.

Best Answer

You can do something along this line:
content1 = fileread( 'm21rmmorphU.txt' ) ;
content2_rows = strsplit( fileread( 'virgoclusterrm.txt' ), sprintf( '\n' )) ;
found = cellfun( @(s)~isempty(strfind(content1, s)), content2_rows ) ;
output_rows = content2_rows(found) ;
fId = fopen( 'similar.txt', 'w' ) ;
fprintf( fId, '%s\n', output_rows{:} ) ;
fclose( fId ) ;