Reputation: 1103
I have .txt file with 300 000 rows. Is there any way to extract specific strings(lines) from that file and save in another .txt or excel only extracted lines ? I talk about log file where i save some requests with time taken for every request. What I want to do is to extract only the time of each request then I'll calculate average time taken.
Hope you guys understand what i talking about.
Edit: Format of .txt file is plain text and every request end with. So i have:
Starting date
//body of response from server
End date
Time: 3150,0792 ms <--- time taken
So, I have 10 000 requests and 10 000 responses. I need to extract only Time of each because will take me a lot of time manually to scroll whole .txt file and check every Time.
Upvotes: 1
Views: 2809
Reputation: 1922
As others already said, it would be useful to have an example of the format. Anyway maybe you can find this tool useful:
http://filehelpers.sourceforge.net/
I use it at work and it allows you to parse and write to different file formats. Hope it helps
Upvotes: 2
Reputation: 35925
You can try using MemoryMappedFile
and TextReader
combined. MMF allows you to access large files and text reader allows you to process file in a line by line manner.
using (var mmf =
MemoryMappedFile.CreateFromFile(@"c:\large.data", FileMode.Open
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
TextReader tr = new StreamReader(stream);
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
Upvotes: 4
Reputation: 6591
private void extract_lines(string filein, string fileout)
{
using (StreamReader reader = new StreamReader(filein))
{
using (StreamWriter writer = new StreamWriter(fileout))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("what you looking for"))
{
writer.Write(line);
}
}
}
}
}
Upvotes: 3
Reputation: 1039160
Sure, you could use StreamReader/StreamWriter:
using (var input = File.OpenText("input.log"))
using (var output = File.CreateText("output.log"))
{
string line;
while ((line = input.ReadLine()) != null)
{
if (SomeConditionOnLine(line))
{
output.WriteLine(line);
}
}
}
This will read the input file line by line, thus having only a single line in memory at once and if this line satisfies some condition that you are looking for write it to the output file. It will be fast and consume very little memory and it will work for huge input files.
Upvotes: 3
Reputation: 11201
you can achieve it by File Class
using (StreamWriter sw = File.AppendText("File2.txt"))
{
foreach (string line in File.ReadLines(@"d:\File1.txt"))
{
if (line.Contains("TheWordInLine"))//This is the line you want by matching something
{
sw.WriteLine("line);
}
}
}
Upvotes: 4