Hi,
I was wondering the best and fastest way to determine how many lines are
in a log file.
At the moment I am simply doing a StreamReader.Re adLine and incrementing
a counter until I reach the end. Is there a better way??
Cheers,
Craig 10 7613
This all depends on how big the log file is. A common method for larger log
files
is to take a statistical sampling and gain an average line length then use the
file size
to compute the number of lines. This is never 100% precise, but does generally
work nicely. If each log line is always of the same length, then you are in
real luck
since then the operation is extremely easy.
As for using StreamReader.Re adLine, that isn't exacty fast. You are creating a
string
object for each line read. You can check for the characters used for line
termination
yourself by using ReadBytes and cycling through the data yourself (not that
hard), but
you'll have to take into account files with different line termination standards
(crlf vs cr vs lf),
since various systems all have their own methods. If this is your log file,
then you can just
search for whatever you've been writing out.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Craig Bumpstead" <cb******@yahoo .com.au> wrote in message
news:Oi******** ******@tk2msftn gp13.phx.gbl... Hi,
I was wondering the best and fastest way to determine how many lines are in a log file.
At the moment I am simply doing a StreamReader.Re adLine and incrementing a counter until I reach the end. Is there a better way??
Cheers, Craig
Craig Bumpstead <cb******@yahoo .com.au> wrote in news:OixsJ$FJEH A.3596
@tk2msftngp13.p hx.gbl: Hi,
I was wondering the best and fastest way to determine how many lines are in a log file.
At the moment I am simply doing a StreamReader.Re adLine and incrementing a counter until I reach the end. Is there a better way??
If you define a line a string of characters ended with a
Environment.New Line, you can thus load the file into a buffer and count the
amounts of Environment.New Line characters (+1 if the last character in the
file isn't an Environment.New Line.)
FB
--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
yeah I was going to say something kinda similar...
StreamReader sW = new StreamReader(fi le);
int lineCount = sW.ReadToEnd(). Split((char)13) .GetUpperBound( 0) + 1;
something like that should work.
On 17/04/2004 "Frans Bouma [C# MVP]" <pe************ ******@xs4all.n l> wrote: Craig Bumpstead <cb******@yahoo .com.au> wrote in news:OixsJ$FJEH A.3596 @tk2msftngp13. phx.gbl:
Hi,
I was wondering the best and fastest way to determine how many lines are in a log file.
At the moment I am simply doing a StreamReader.Re adLine and incrementing a counter until I reach the end. Is there a better way??
If you define a line a string of characters ended with a Environment.Ne wLine, you can thus load the file into a buffer and count the amounts of Environment.New Line characters (+1 if the last character in the file isn't an Environment.New Line.)
FB
Wow, I definitely would not use that method. Looks pretty to say the least, but
creates a very large amount of extra baggage. ReadToEnd() creates one huge
string. Split repackages that data into a string for every single line. Big
memory
waste at this point. Since Split will return a string array with even the last
line you
should just need a Length call.
Here is a more performant version for large files that uses a sharing
FileStream.
I've also included an updated version of the ReadToEnd method. You can easily
add some timing code in and create a rather large file that demonstrates the
first
method being faster and more memory efficient.
using System;
using System.IO;
public class LineCount {
private static byte[] lineBuffer = new byte[4196]; // 4K
private static void Main(string[] args) {
int lines = 0;
using(FileStrea m fs = new FileStream(args[0], FileMode.Open,
FileAccess.Read , FileShare.Read, lineBuffer.Leng th)) {
int bufferRead = 0;
while( (bufferRead = fs.Read(lineBuf fer, 0, lineBuffer.Leng th)) >
0 ) {
for(int i = 0; i < bufferRead; i++) {
if ( lineBuffer[i] == 0xD ) {
lines++;
}
}
}
fs.Close();
}
lines++;
Console.WriteLi ne(lines);
StreamReader sw = new StreamReader(ar gs[0]);
lines = sw.ReadToEnd(). Split((char)13) .Length;
Console.WriteLi ne(lines);
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Scatropoli s" <ch*****@frayed .net> wrote in message
news:OP******** ******@tk2msftn gp13.phx.gbl...
yeah I was going to say something kinda similar...
StreamReader sW =ew StreamReader(fi le);
int lineCount =W.ReadToEnd(). Split((char)13) .GetUpperBound( 0) + 1;
something like that should work.
On 17/04/2004 "Frans Bouma [C# MVP]" <pe************ ******@xs4all.n l> wrote: Craig Bumpstead <cb******@yahoo .com.au> wrote in news:OixsJ$FJEH A.3596 @tk2msftngp13. phx.gbl:
Hi,
I was wondering the best and fastest way to determine how many lines are in a log file.
At the moment I am simply doing a StreamReader.Re adLine and incrementing a counter until I reach the end. Is there a better way??
If you define a line a string of characters ended with a Environment.Ne wLine, you can thus load the file into a buffer and count the amounts of Environment.New Line characters (+1 if the last character in the file isn't an Environment.New Line.)
FB
Thanks everybody for the advice,
The files that I have been reading are about 1 to 3 Gb in size.
So as you could imagine that the ReadLine takes some time to complete.
I wanted the amount of lines in a file so that I could then use it for
the calc. of the progress bar.
Cheers,
Craig
Justin Rogers wrote: Wow, I definitely would not use that method. Looks pretty to say the least, but creates a very large amount of extra baggage. ReadToEnd() creates one huge string. Split repackages that data into a string for every single line. Big memory waste at this point. Since Split will return a string array with even the last line you should just need a Length call.
Here is a more performant version for large files that uses a sharing FileStream. I've also included an updated version of the ReadToEnd method. You can easily add some timing code in and create a rather large file that demonstrates the first method being faster and more memory efficient.
using System; using System.IO;
public class LineCount { private static byte[] lineBuffer = new byte[4196]; // 4K private static void Main(string[] args) { int lines = 0; using(FileStrea m fs = new FileStream(args[0], FileMode.Open, FileAccess.Read , FileShare.Read, lineBuffer.Leng th)) { int bufferRead = 0; while( (bufferRead = fs.Read(lineBuf fer, 0, lineBuffer.Leng th)) > 0 ) { for(int i = 0; i < bufferRead; i++) { if ( lineBuffer[i] == 0xD ) { lines++; } } } fs.Close(); } lines++; Console.WriteLi ne(lines);
StreamReader sw = new StreamReader(ar gs[0]); lines = sw.ReadToEnd(). Split((char)13) .Length; Console.WriteLi ne(lines); } }
Craig, since you are only displaying a progress bar, then you want an average
measurement. I would highly recommend using the method I show below,
with some form of cut-off. For example:
(Read 16k worth of data, 4 times through the loop). Then:
float fudge = 1.05f;
totalLines = (int) (averagedLines * (FileLength / 16k) * fudge);
If you are reading the total number of lines first, then you are already
processing the entire file. There are fast ways to do this (as I've shown
below)
and slow ways, but you need a way that doesn't force you to read the entire
file,
and instead guess at the total number of lines.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Craig Bumpstead" <cb******@yahoo .com.au> wrote in message
news:et******** ******@TK2MSFTN GP09.phx.gbl... Thanks everybody for the advice,
The files that I have been reading are about 1 to 3 Gb in size. So as you could imagine that the ReadLine takes some time to complete. I wanted the amount of lines in a file so that I could then use it for the calc. of the progress bar.
Cheers, Craig
Justin Rogers wrote:
Wow, I definitely would not use that method. Looks pretty to say the least,
but creates a very large amount of extra baggage. ReadToEnd() creates one huge string. Split repackages that data into a string for every single line.
Big memory waste at this point. Since Split will return a string array with even the
last line you should just need a Length call.
Here is a more performant version for large files that uses a sharing FileStream. I've also included an updated version of the ReadToEnd method. You can
easily add some timing code in and create a rather large file that demonstrates the first method being faster and more memory efficient.
using System; using System.IO;
public class LineCount { private static byte[] lineBuffer = new byte[4196]; // 4K private static void Main(string[] args) { int lines = 0; using(FileStrea m fs = new FileStream(args[0], FileMode.Open, FileAccess.Read , FileShare.Read, lineBuffer.Leng th)) { int bufferRead = 0; while( (bufferRead = fs.Read(lineBuf fer, 0, lineBuffer.Leng th))
0 ) { for(int i = 0; i < bufferRead; i++) { if ( lineBuffer[i] == 0xD ) { lines++; } } } fs.Close(); } lines++; Console.WriteLi ne(lines);
StreamReader sw = new StreamReader(ar gs[0]); lines = sw.ReadToEnd(). Split((char)13) .Length; Console.WriteLi ne(lines); } }
In the interest of providing a complete example for this: http://weblogs.asp.net/justin_rogers...17/115346.aspx
and http://weblogs.asp.net/justin_rogers...es/115345.aspx
The first link is the introduction to the article and the second link is an
article detailing the various concepts behind statistical line counting along
with full source code at the end.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl... Craig, since you are only displaying a progress bar, then you want an average measurement. I would highly recommend using the method I show below, with some form of cut-off. For example:
(Read 16k worth of data, 4 times through the loop). Then:
float fudge = 1.05f; totalLines = (int) (averagedLines * (FileLength / 16k) * fudge);
If you are reading the total number of lines first, then you are already processing the entire file. There are fast ways to do this (as I've shown below) and slow ways, but you need a way that doesn't force you to read the entire file, and instead guess at the total number of lines.
-- Justin Rogers DigiTec Web Consultants, LLC. Blog: http://weblogs.asp.net/justin_rogers
"Craig Bumpstead" <cb******@yahoo .com.au> wrote in message news:et******** ******@TK2MSFTN GP09.phx.gbl... Thanks everybody for the advice,
The files that I have been reading are about 1 to 3 Gb in size. So as you could imagine that the ReadLine takes some time to complete. I wanted the amount of lines in a file so that I could then use it for the calc. of the progress bar.
Cheers, Craig
Justin Rogers wrote:
Wow, I definitely would not use that method. Looks pretty to say the
least, but creates a very large amount of extra baggage. ReadToEnd() creates one huge string. Split repackages that data into a string for every single line. Big memory waste at this point. Since Split will return a string array with even the last line you should just need a Length call.
Here is a more performant version for large files that uses a sharing FileStream. I've also included an updated version of the ReadToEnd method. You can easily add some timing code in and create a rather large file that demonstrates
the first method being faster and more memory efficient.
using System; using System.IO;
public class LineCount { private static byte[] lineBuffer = new byte[4196]; // 4K private static void Main(string[] args) { int lines = 0; using(FileStrea m fs = new FileStream(args[0], FileMode.Open, FileAccess.Read , FileShare.Read, lineBuffer.Leng th)) { int bufferRead = 0; while( (bufferRead = fs.Read(lineBuf fer, 0,
lineBuffer.Leng th)) 0 ) { for(int i = 0; i < bufferRead; i++) { if ( lineBuffer[i] == 0xD ) { lines++; } } } fs.Close(); } lines++; Console.WriteLi ne(lines);
StreamReader sw = new StreamReader(ar gs[0]); lines = sw.ReadToEnd(). Split((char)13) .Length; Console.WriteLi ne(lines); } }
Justin,
Thanks for that bit of code.
It took 2 mins 8 sec to read the 3 Gb file with 12,656,376 lines.
The readline technique had only gotten up to 1,905,686 after 10 mins.
My machine:
2 x AMD Athlon MP 2400
1 Gb RAM
80Gb IDE HD
I was wondering if I should make it a thread so that I can start
proceessing the file?
Cheers,
Craig
Justin Rogers wrote: Craig, since you are only displaying a progress bar, then you want an average measurement. I would highly recommend using the method I show below, with some form of cut-off. For example:
(Read 16k worth of data, 4 times through the loop). Then:
float fudge = 1.05f; totalLines = (int) (averagedLines * (FileLength / 16k) * fudge);
If you are reading the total number of lines first, then you are already processing the entire file. There are fast ways to do this (as I've shown below) and slow ways, but you need a way that doesn't force you to read the entire file, and instead guess at the total number of lines.
Definitely try some of the code I posted on my blog. What I would recommend
for a 3GB file is processing approximately 1 meg of that. That would only be
256
4K blocks. Check that value against your line count and see if it is relatively
close.
It should be and will take less than a second to process.
By setting the access mode to read and the share mode to read, you could process
the lines in a separate thread while starting the processing. However, you are
going
to incur double the disk access, which is why i think you need to use the
statistical
methods and bring your parsing time down.
I'm very interested in helping solve this particular problem in a performant
way, so
feel free to contact me through my blog if you run into any issues.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Craig Bumpstead" <cb******@yahoo .com.au> wrote in message
news:OL******** ******@TK2MSFTN GP09.phx.gbl... Justin,
Thanks for that bit of code.
It took 2 mins 8 sec to read the 3 Gb file with 12,656,376 lines. The readline technique had only gotten up to 1,905,686 after 10 mins.
My machine: 2 x AMD Athlon MP 2400 1 Gb RAM 80Gb IDE HD
I was wondering if I should make it a thread so that I can start proceessing the file?
Cheers, Craig
Justin Rogers wrote: Craig, since you are only displaying a progress bar, then you want an
average measurement. I would highly recommend using the method I show below, with some form of cut-off. For example:
(Read 16k worth of data, 4 times through the loop). Then:
float fudge = 1.05f; totalLines = (int) (averagedLines * (FileLength / 16k) * fudge);
If you are reading the total number of lines first, then you are already processing the entire file. There are fast ways to do this (as I've shown below) and slow ways, but you need a way that doesn't force you to read the entire file, and instead guess at the total number of lines.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Xah Lee |
last post by:
suppose you want to do find & replace of string of all files in a
directory.
here's the code:
©# -*- coding: utf-8 -*-
©# Python
©
©import os,sys
©
©mydir= '/Users/t/web'
|
by: SirPoonga |
last post by:
Can I determine how many lines an xml file has so I can say something
like "line 4 of 254"?
|
by: Wei Su |
last post by:
Hi,
I have a text file abc.txt and it looks like:
12 34 56
23 45 56
33 56 78
... .. ..
... .. ..
I want to get how many rows totally in the text file, how to do this?
Thanks.
|
by: Neo Geshel |
last post by:
This works:
<form>
<asp:TextBox id="name" />
<%= name.ClientID %>
</form>
But this DOES NOT work:
<form>
|
by: CSharpGuy |
last post by:
I'm creating a Excel spreadsheet and I need to add a total of how many lines
were added to the spreadsheet. How can I keep a running total and then show
the total of how many lines were added to the spreadsheet?
| |
by: peter |
last post by:
Hello all,
I'm looking for an advice.
Example (one block in ascii file):
$------------------------
NAME='ALFA'
CODE='x'
$------------------------
|
by: karinmorena |
last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is:
Week5MortgageGUI.java:151:cannot find symbol
symbol: method allInterest(double,double,double)
Location: class Week5MortgageGUI
Week5MortgageLogic allint = logic.allInterest(amount, term, rate);
Week5MortgageGUI.java:152:cannot find symbol
symbol: method allInterest(double,double,double)
Location: class Week5MortgageGUI
|
by: W. eWatson |
last post by:
I have an ordinary text file with a CR at the end of a line, and two numbers
in each line. Is there some way to determine the number of lines (records)
in the file before I begin reading it?
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
|
by: eraserwars |
last post by:
My compiler keeps saying LNK2019, and my teacher says to look for spelling error. He says that most likely what is happening is that a spelling error is messing my program up. I searched, and I did not find a spelling error though...
This is what my error message looks like
error LNK2019: unresolved external symbol "public: __thiscall weatherStationSystem::weatherStationSystem(void)" (??0weatherStationSystem@@QAE@XZ) referenced in function...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |