Identify a character in a file and edit the file from that character.

1 次查看(过去 30 天)
Hi,
I have a file like this:
v 347.656 -229.91 -677.344
v 218.75 -97.6563 -637.29
v 171.875 -31.25 -614.025
v 113.281 -47.0539 -618.75
v 181.448 -183.594 -661.719
v 97.6563 27.1677 -708.594
v 212.596 -50.7813 -653.906
v 155.339 -171.875 -614.844
v 62.5 -105.469 -685.376
v 292.969 -242.458 -692.969
v 105.207 15.625 -642.188
v 222.656 -238.281 -655.06
v 253.906 -93.6077 -716.406
v 210.938 19.5313 -829.752
v 43.6444 -164.063 -685.156
v 128.906 -78.125 -607.902
v 70.3125 -199.219 -650.156
v 50.7813 -257.813 -653.715
v 199.219 -212.997 -673.438
v 54.6875 -57.0634 -696.875
v 160.156 -39.0625 -619.608
# 13583 elements written
vn 0.0387398 0.926983 -0.373098
vn 0.83673 -0.0302438 0.54678
vn 0.0452287 -0.381464 0.923277
vn -0.0127617 -0.51591 0.856547
vn 0.94213 0.00369861 0.335228
vn 0.44112 -0.694334 0.568606
vn 0.63703 -0.652082 -0.411074
vn 0.498461 -0.851513 -0.162671
vn 0.822075 -0.11604 0.557429
vn -0.515863 0.473125 0.714169
vn 0.46519 -0.762386 0.449852
vn -0.478219 0.778441 0.406615
vn 0.325099 -0.500174 -0.802581
vn 0.0148388 0.0119385 0.999819
vn 0.489283 0.429513 0.759026
The numbers of lines are variable. What I need is to identify the "#" character and delete that line and all the lines under this one. Or copy all the lines above this one into a new file.
There is another character "#" in the file if it's a problem.

采纳的回答

Daniel Shub
Daniel Shub 2012-11-28
I don't think I would use MATLAB to do this since learning sed and/or awk is really useful ... A way to do it in MATLAB
fid = fopen('data.txt');
data = textscan(fid, '%[^#]');
fclose(fid);
fid = fopen('data.txt', 'w');
fprintf(fid, '%s\n', data{1}{:})
fclose(fid);
  1 个评论
Jose
Jose 2012-11-28
Thank you so much!!!
It did just what I was waiting for! Well I had to do a little modification with the bufsize because of the hughe size of my files, but it worked perfectly!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by