How can I efficiently search for a specific string in a large text file using C#? - c#

I have a large text file (over 10 GB) and I need to find all occurrences of a specific string in it. I am currently using a StreamReader and looping through each line to look for the string, but this is very slow and takes hours to complete. What is a more efficient way to search for the string in the file using C#?
I've tried using StreamReader to loop through each line of the text file and using string.Contains() to look for the string. I've also tried splitting the file into smaller chunks and using multiple threads to search each chunk, but this did not improve the performance significantly.

Related

C# Reading a specific line from large data .txt file

I have a txt file in which X,Y coordinates are saved. Each lines contains different coordinates, as a result each line might have different size from the previous/next line.
The size of the file is too large to open it uses ReadAllLines function ( File can be larger than 10-50GB ). I have seen many answers talking about File.ReadLines(filename).Skip(n).Take(n).ToList();
My questions are, will this method load the file on RAM or it will load only the lines inside the Take(n) function?
If there is no way to access directly a specific line in txt file, would it be a good idea to transfer the data into a database table where access is easier?
Thanks in advance,
The microsoft documentation says: "The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned"
I Think you suggestion will load only the lines in the RAM.
Test it and use the memory debugger to check it Microsoft Doku
Maybe you can remove some overhead of this big file ...

Most efficient and quickest way to search numerous 1Gb text files for a certain string or instances of that string?

For work I am tasked with finding out how many times a certain set of sequential characters are used in a string. These files are all 1GB + in size and there are anywhere from 4 to 15 files like these. ex. find "cat" in "catastrophe" in every instance "cat" is part of the word in every file.
In theory (at least to me) I would load one text file into memory and then line by line look for the match. At the end of the text file I would remove it from memory and load the next text file.... until all files have been searched.
I have been doing mostly script files to automate tasks these last few years and I have been out of the coding game for so long, I don't remember, or maybe I never knew, the most efficient and fastest way to do this.
When I say speed I mean in elapsed time of the program, not how long it will take me to write this.
I would like to do this in C# because I am trying to get more comfortable with the language but really I could do it in any language. Preferably not assembly ...that was a joke...

Large Text Files versus Small tex files

This is just a general question? I have an application that uses the File.ReadAllLines and i was wondering, is it easier to load as one big file or multiple small files.
My current file is a Dictionary for my speech recognition. Currently it has about 40,000 lines in it. When i start the program it takes about 2 minutes for it to load the grammars.
When the file is less than 1,000 lines it processes and reads all line instantly. When added phases two implementing the Entire King James Bible. It slowed down tremendously.
So for better performance would be better to have multiple smaller files instead of one big file? I will be adding 16 more txt documents with each file roughly 2,000 to 3,000 lines in each txt document.
If its easier and faster to have several smaller then i wont bother adding them to one big file.
I am building a custom Speech Dictation engine, so it will consist of The KJV Bible, names, numbers and different NLP variations. Eventually i will be adding the Exhaustive Strong Concordance. So far the program works perfectly. I am just trying to get faster load times, on the initial start up.

Handling large text files in C#

Hey there! I am needing to read large text files up to 100mb in size. I need to read each line Search for a string and write the results to a log. What would be the best way of doing this? Should I read each line individually and search it then move on to the next one?
Allocating a string of up to 200mb isn't that much at all these days. Just read it all at once and process it.
One option is to use memory mapped files.

Options for header in raw byte file

I have a large raw data file (up to 1GB) which contains raw samples from a USB data logger.
I need to store extra information relating to the file (sample rate, description, trigger point, last seek position etc) and was looking into adding this as a some sort of header.
The header file should ideally be human readable and flexible so I've so far ruled out some sort of binary serialization into a header.
I also want to avoid two separate files as they could end up separated when copied or backed up. I remembered somebody telling me that newer *.*x Microsoft Office documents are actually a number of files in a zip. Is there a simple way to achieve this? Could I still keep the quick seek times to the raw file?
Update
I started using the binary serializer and found it to be a pain. I ended up using the xml serializer as I'm more comfortable using it.
I reserve some space at the start of the files for the xml. Simple
When you say you want to make the header human readable, this suggests opening the file in a text editor. Do you really want to do this considering the file size and (I'm assuming), the remainder of the file being non-human readable binary data? If it is, just write the text header data to the start of the binary file - it will be visible when the file is opened but, of course, the remainder of the file will look like garbage.
You could create an uncompressed ZIP archive, which may allow you to seek directly to the binary data. See this for information on creating a ZIP archive: http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

Categories

Resources