How to delete a specific line from text file?

43 次查看(过去 30 天)
I got this text file:
3033011 Penelope_Cruz Cnm 99 88 77
404402 Gary_Cooper Bio 45 45 45
5055022 Tony_Curtis Dis 23 89 100
606603 Jim_Carrey Mng 100 100 100
7070000 Sandra_Bulloc Cnm 99 45 89
898989 Halle_Berry Bio 75 75 75
and I want to delete the 3rd line.
how can I do it?

采纳的回答

Walter Roberson
Walter Roberson 2015-6-4
You might notice that the below is not a nice little 3 or 5 line program. That is because the operating systems that MATLAB is supported on do not offer any method of deleting bytes in the middle of a file. The only real option is to copy the wanted text to another file and rename the files around.
Some of what is in this code is error checking and making sure that there is always an intact version of the file. If something goes wrong, you don't want to end up with a corrupted file. Always keep a backup version until you know the the deletion did what you wanted (and only what you wanted)
first_line_to_delete = 3;
num_lines_to_delete = 1;
infilename = 'YourFile.txt';
[pathstr, file, ext] = fileparts(infilename);
backfile = fullfile(pathstr, [file '.bak']);
if strcmp(infilename, backfile)
error('I refuse to edit a backup file! Nothing has been changed.');
end
outfile = tempfile; %a temporary file in TMP directory
fin = fopen(infilename, 'r');
if fin < 0; error('Input file does not exist'); end
fout = fopen(tfile, 'w');
if fout < 0
fclose(fin);
error('Could not open temporary output file');
end
%read lines before the one to be deleted and write them to output
for K = 1 : first_line_to_delete - 1;
inline = fgets(fin);
if ~ischar(inline); break; end; %end of file?
fwrite(fout, inline);
end
for K = 1 : num_lines_to_delete
if ~ischar(inline); break; end %in case EOF
inline = fgets(fin); %and do nothing with it
end
%copy all remaining input lines to output file
while ischar(inline)
inline = fgets(fin);
if ischar(inline) %not if we hit EOF
fwrite(fout, inline);
end
end
fclose(fin);
fclose(fout);
%M'Kay, we did the copying and have a file with the desired
%result. Now put it in the proper place
status = movefile(infilename, backfile);
if ~status
if strcmp(infilename, backfile)
fprintf(2, 'Good thing your programmer is paranoid about people overriding\nsanity checks, because something went wrong and you nearly lost your file!\n');
else
delete(backfile);
end
error('Could not rename file to .bak, file left untouched');
else
status = movefile(outfile, infilename);
if ~status
error( ['Could not rename temp file to original name, original moved to ', backfile]);
end
end
  6 个评论
ben velt
ben velt 2015-6-4
I guess I didnt explained myself very well. My program is like this- Im opening the List.txt file with matlab into a cell array table. Then I need to decide if I want to delete someone from this table. After I delete from the cell array the cells that I dont need, I need to change the List.txt file also, according to the changes I did.
Walter Roberson
Walter Roberson 2015-6-5
Then you don't have to worry about deleting lines from text files: just delete the cells from the table and then write out the table.

请先登录,再进行评论。

更多回答(1 个)

per isakson
per isakson 2015-6-4
An alternate approach
function cssm( )
str = fileread( 'cssm.txt' );
pos = strfind( str, sprintf('\n') );
str( pos(2)+1 : pos(3) ) = [];
fid = fopen( 'cssm1.txt', 'w' );
fprintf( fid, '%s', str );
fclose( fid );
end
Tested on R2013a

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by