String search
12 次查看(过去 30 天)
显示 更早的评论
How I can search for a string in a text file and delete all the lines except the lines that have the specific string. Thanks.
0 个评论
回答(3 个)
Honglei Chen
2012-3-4
It is probably easier to write all lines that have the specific string into a new file so you don't have to worry about the file pointer location.
You use fopen to open the file, use fgetl to get the line. Then you can use strfind to check if it has the specific string. If so, write it to the new file using fprintf.
doc fopen
doc fgetl
doc strfind
doc fprintf
Here is an example
fid1 = fopen('old.txt','r');
fid2 = fopen('new.txt','w');
while 1
tline = fgetl(fid1);
if strfind(tline,SPECIFI_STRING)
fprintf(fid2,'%s\n',tline);
end
end
fclose(fid1);
fclose(fid2);
Walter Roberson
2012-3-5
Read the file in as a complete string, regexp() to find the lines that match the string, write out the matched lines.
OR
Write a very small perl script whose essential code would be
{print if /TheString/}
There is some overhead to invoke it nicely from MATLAB rather than using system(), as system() would allow you the very compact
system('perl -ne "{print if /TheString/}" < InputFile > OutputFile')
Anyhow, I showed an example of the longer overhead at http://www.mathworks.com/matlabcentral/answers/27888-writing-nan-values-to-text-files
0 个评论
sara
2012-3-6
1 个评论
Walter Roberson
2012-3-6
Are you looking through an HTML stream for a Content-type: video/SOMETHING
http://www.w3.org/TR/html4/types.html#h-6.7
If you are, then you need to know that the representation of the video will *not* have one such header for each line. Instead, there will be a Content-transfer-encoding header that specifies an encoding type such as base64; then after the end of the mime-headers, there will be an empty line, and then there will be the encoded data for the video. If base64 encoding is used, there will be NO per-line tag indicating the line has video: it will simply be a line of up to 76 encoded characters.
I do not know at the moment what else you might mean by "video tags".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!