How to extract only parts of a text file? not just skip a few lines but extracting segments found in all text of file.
3 ビュー (過去 30 日間)
古いコメントを表示
Hello.
I'm been trying to extract only certain parts of a .txt file without success.
The file is way to big to post any part of, but for example. When you compile a arduino sketch and you copy the output shown in the in the bottom of the IDE window and paste it into a .txt file you get number of very long lines. Somewhere in those lines there is a path to the directory where the IDE use some files for example:
-IC:\Program Files (x86)\Arduino\hardware\arduino\sam\system/libsam -IC:\Program Files...
following that there are a few more paths in each line but before that there are a lot of other garble, commands and so.
I want to extract only the C:\ paths, I thought maybe I could specify to read everything starting with '-IC:\Program Files (x86)\' and ends with ' ', there is a blank space between /libsam and -IC... above.
But I can't find any straight way of doing that and my attempts so far has not yielded any desirable result.
So far I have gotten this:
fileName = 'DMA.txt';
data = fileread(fileName);
s = strfind(data, '-IC:');
then if I do, EDU>> whos data
Name Size Bytes Class Attributes
data 1x34585 69170 char
and s is a 1x140 double, then I got the starting point of every part of the text that I want and I have checked how many characters after each starting point is of interest and it varies but it is always less then 100.
I have almost a answer I think, the only problem is how to store 140 different strings. t = data(s(1):s(2)) gives the first occurrence so a for loop like
for i = 1:length(s)
str = data(s(i):s(i+1);
end
would almost do it, but I don't know how to handle the str variable? It needs to be an array of strings but nothing I do result in that.
I attach the file in question.
Regards
3 件のコメント
dpb
2014 年 12 月 3 日
How large are the files overall? It often is simplest to snarf up the whole file intact into memory and then work on it there.
How many lines/path occurrences are there and are they all or only a specific one or few of interest?
Regular expressions are often useful in such contexts...
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!