MATLAB: How to load text file ? delete some specific part of the text file and then write the remaining as it is into the text file

fopen

suppose the if input text file contains lines of characters as shown
inputfile.txt
12 4 5 678 6g4g4g5r6r7r9
12 5 79 hghoojb iumkk
fhjk kjjjlk g2g3g5g4r5r7r8
12 4 80999b9 77908900
12 4 5 678 6g4g4g5r6r7r9
i need output file which has no characters after r for every specific line . like this
outputfile.txt
12 4 5 678 6g4g4g5
12 5 79 hghoojb iumkk
fhjk kjjjlk g2g3g5g4
12 4 80999b9 77908900
12 4 5 678 6g4g4g5
please help me with this?

Best Answer

filecontent = fileread('inputfile.txt');
outcontent = regexprep( filecontent, 'r.*$', '', 'dotexceptnewline', 'lineanchors');
fid = fopen('outfile.txt', 'w');
fwrite(fid, outcontent);
fclose(fid);
Related Question