how to read last few rows/lines from a text file ?

74 ビュー (過去 30 日間)
missy QIANHUI
missy QIANHUI 2011 年 10 月 27 日
回答済み: Kristen Cognac 2022 年 10 月 20 日
how to read last few rows/lines from a text file ? thanks

回答 (4 件)

Bjorn Gustavsson
Bjorn Gustavsson 2011 年 10 月 27 日
編集済み: Walter Roberson 2016 年 6 月 20 日
If you can use something like this much is won:
[q,w] = system(['tail -n ',num2str(nr_of_lines_to_read),' ',filename]);
Then it can be as simple as this:
w = str2num(w);
...or more complicated depending on the contents of your file.
HTH.
  3 件のコメント
missy QIANHUI
missy QIANHUI 2011 年 11 月 10 日
ok thanks. i go try it out
K E
K E 2016 年 6 月 20 日
tail command doesn't work on Windows machines. Cygwin may be a workaround.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 10 月 27 日
Unless you can set a strict upper limit on the number of characters per line, the only way is to read a line at a time, remembering each new line and throwing away the oldest line (if you already have enough lines in the buffer.)
L = fgetl(fid);
if ~ischar(L); break; end %end of file
if length(LS) >= N %we have an extra line buffered
LS(1) = []; %so delete it
end
LS{end+1} = L; %and remember this new current last line
There are other ways of proceeding but they generally end up reading in the entire file first and then throwing away everything extra.
If you can put a strict limit on the number of characters per line, then there is a short-cut you can use: if the maximum line length is M and you want the last N lines, fseek() backwards (M+2)*N bytes from the end of the file and then start the above reading process. The +2 is to count carriage return and linefeeds.
I should specify that this shortcut of fseek() only works if your characters are single byte, no extended characters like greek or malaysian. If your file is UTF-8 or UTF-16 or UTF-32 encoded, then it is possible to make adjustments to the backwards seek count, but you could end up in the middle of an encoded character and you have to account for that.
  1 件のコメント
missy QIANHUI
missy QIANHUI 2011 年 10 月 27 日
ok thank you i go try it out

サインインしてコメントする。


Amardeep
Amardeep 2011 年 10 月 27 日
You could used a fscan eof to get to the end of file then fgetl step up a line fgetl and repeat until you have enough lines from the end of the file.
  1 件のコメント
missy QIANHUI
missy QIANHUI 2011 年 10 月 27 日
ok thank you i go try it out

サインインしてコメントする。


Kristen Cognac
Kristen Cognac 2022 年 10 月 20 日
Depends what operating system you're using. For Windows, I've had luck calling powershell and utilizing the Get-Content and Tail commands. Note, previously I was using fget1 and had to scan >10 million of text lines which took several minutes For this example, I've grabbed the last line of text with two lines of code and the same 10+ million line text file is read in 0.417 seconds. I'm sure you could condense it to one line as well...
Generic example:
temp_var=system('powershell -inputformat none cd YourFolderLocation');
[q,w] = system('powershell -inputformat none Get-Content *YourFileName* -Tail "#lines"');
Example with file names accessing last line and showing actual format:
temp_var=system('powershell -inputformat none cd C:\Users\XX\Downloads');
[q,w] = system('powershell -inputformat none Get-Content *Example.txt* -Tail "1"');

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by