Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I Have a text file with huge data. I want to read the last 150 lines of data and within the 150 lines of data i want to filter the data based on some partiucular search criteria.

Any Help...
Posted
Comments
Herman<T>.Instance 2-Oct-13 1:42am    
what have you tried? Please show us the code where you are stuck

It is easy to see that is is impossible to read last N lines without reading all previous lines. This is because the lines have different lengths which you don't know in advance. Before you read them all, you don't know the location of the next line in the file. Moreover, you need to keep at least 150 lines in memory, because you don't know how many lines are there. The simplest method is this:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx[^].

When you have all lines into the array, you use the last 150 lines if the number of array elements is greater or equal to 150, or all lines otherwise.

Huge data? I hope due to many lines, not because the lines are too long, otherwise 10 lines could be too much. Then, the idea is: make a window of 150+150 lines, read lines into a window of 150 lines, which you may or may not use. Read the file in chunks of 150. The second array is needed in case your last chunk is less then 150 (very likely, right?). Then you will need data from the previous window.

Alternative solution: read all lines but don't remember them, only remember positions of each line in file. After this loop, you will know the number of lines and positions of them in file. Then you will need a second loop: read only the lines you need.

—SA
 
Share this answer
 
v2
As I see, you want something similar to linux tail utility. Check this: linux "tail -f" Command in C#.NET[^]
 
Share this answer
 
You can try bellow code syntax.

List<string> text = File.ReadLines("file.txt").Reverse().Take(2).ToList();

for more information visit this URL.
 
Share this answer
 
Simply, if you know the number of lines, i.e. how many lines inside your file,
then apply a for() loop, starting from the last line, count backward by 150,
e.g.:

Java
String[] SomeBuffer = new String[150];
int j=0;
for (int i=NumberOfLines; i > (NumberOfLines - 150); i--)
{
   // Copy the lines into SomeBuffer[j];
   j++;
}

You should be careful about the Line index numbers.
For example, idx=0,1,2,3 means 4 records, not 3.

Also it would be better to do some preliminary calculations before you read the lines,
like how many lines? or what's the maximum length of a line?, etc.
( agree with Sergey Kryukov and others that you should provide more information)
 
Share this answer
 
v2
hi,
you may try like this:
C#
string tempText = "file text containing more than 150 line of text";
                string result = string.Empty;

                string[] lines = tempText.Split('\n');//this will break all string for each line

                if (lines.Length > 150)
                {
                    for (int i = lines.Length - 150; i < lines.Length; i++)
                    {
                        //get lines here like
                        result = lines[i];
                    }
                }
 
Share this answer
 
Comments
nm.nagaraju 2-Oct-13 2:01am    
Hi My question is that I want to read the "last 150 lines (from the bottom)" of data within the text file.
tanweer 2-Oct-13 2:08am    
yeah you can get the last 150 line in the for loop, try concatenating the result variable like:
result +=lines[i];
nm.nagaraju 2-Oct-13 2:36am    
string[] lines = tempText.Split('\n');

The above statement will return only one line, even the text file is having more than 150 lines.
tanweer 2-Oct-13 2:42am    
try
string[] lines = tempText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

i was supposing that you had saved the content from an editor pressing <enter> for each line end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900