Fastest way to find strings in a file

fastest way to search for a string in a large text file python
python find string in file and print line
python fast file search
search for a pattern in a file python
python search string in multiple files
how to grep a word in a file in python
python grep large file
python grep string from variable

I have a log file that is not more than 10KB (File size can go up to 2 MB max) and I want to find if atleast one group of these strings occurs in the files. These strings will be on different lines like,

ACTION:.......

INPUT:...........

RESULT:..........

I need to know atleast if one group of above exists in the file. And I have do this about 100 times for a test (each time log is different, so I have reload and read the log), so I am looking for fastest and bets way to do this.

I looked up in the forums for finding the fastest way, but I dont think my file is too big for those silutions.

Thansk for looking.

I would read it line by line and check the conditions. Once you have seen a group you can quit. This way you don't need to read the whole file into memory. Like this:

    public bool ContainsGroup(string file)
    {
        using (var reader = new StreamReader(file))
        {
            var hasAction = false;
            var hasInput = false;
            var hasResult = false;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (!hasAction)
                {
                    if (line.StartsWith("ACTION:"))
                        hasAction = true;
                }
                else if (!hasInput)
                {
                    if (line.StartsWith("INPUT:"))
                        hasInput = true;
                }
                else if (!hasResult)
                {
                    if (line.StartsWith("RESULT:"))
                        hasResult = true;
                }

                if (hasAction && hasInput && hasResult)
                    return true;
            }
            return false;
        }
    }

This code checks if there is a line starting with ACTION then one with INPUT and then one with RESULT. If the order of those is not important then you can omit the if () else if () checks. In case the line does not start with the strings replace StartsWith with Contains.

How to use grep command in UNIX / Linux {With Examples}, How do I search for a file in a string? To make use of strings in your program you need to make sure that you include the header file string.h #include<stdio.h> #include<stdlib.h> #include<string.h> Now that we have the right headers files included we can start with our main function:

Here's one possible way to do it:

StreamReader sr;
string fileContents;

string[] logFiles = Directory.GetFiles(@"C:\Logs");

foreach (string file in logFiles)
{

    using (StreamReader sr = new StreamReader(file))
    {

        fileContents = sr.ReadAllText();

        if (fileContents.Contains("ACTION:") || fileContents.Contains("INPUT:") || fileContents.Contains("RESULT:"))
        {
             // Do what you need to here
        }

    }
}

You may need to do some variation based on your exact implementation needs - for example, what if the word spans two lines, does the line need to start with the word, etc.

Added

Alternate line-by-line check:

StreamReader sr;
string[] lines;

string[] logFiles = Directory.GetFiles(@"C:\Logs");

foreach (string file in logFiles)
{

    using (StreamReader sr = new StreamReader(file)
    {

        lines = sr.ReadAllLines();

        foreach (string line in lines)
        {        
            if (line.Contains("ACTION:") || line.Contains("INPUT:") || line.Contains("RESULT:"))
            {
                // Do what you need to here
            }
        }

    }
}

Use Notepad++ to find text in all files of a folder, How do you search all files in a directory for a string in Windows? What is the fastest technology/algorithm that can be implemented in order to lookup list of strings in a quite large text files (Up to 1GB text files). For starters, I'm using C# and was able to achieve the logic (Simply by matching a file with list of strings, string by string every time.

Take a look at How to Read Text From a File. You might also want to take a look at the String.Contains() method.

Basically you will loop through all the files. For each file read line-by-line and see if any of the lines contains 1 of your special "Sections".

Search word in text file using java, How do I search for a file containing a specific text in Linux? Findstr is a powerful command that you may use to search for strings in files or to filter command line output. You may use it to scan entire directory structures or drives for files that match the selected string or part of it, and to find specified text in command line outputs quickly.

You don't have much of a choice with text files when it comes to efficiency. The easiest way would definitely be to loop through each line of data. When you grab a line in a string, split it on the spaces. Then match those words to your words until you find a match. Then do whatever you need.

I don't know how to do it in c# but in vb it would be something like...

Dim yourString as string
Dim words as string()
Do While objReader.Peek() <> -1
   yourString = objReader.ReadLine()
   words = yourString.split(" ")
   For Each word in words()
      If Myword = word Then
         do stuff
      End If
   Next
Loop

Hope that helps

What is the fastest (accurate) way to search for a string of text in files , How do you check if a word is present in a file in Java? The following code uses regular expressions to validate the format of each string in an array. The validation requires that each string have the form of a telephone number in which three groups of digits are separated by dashes, the first two groups contain three digits, and the third group contains four digits.

This code sample searches for strings in a large text file. The words are contained in a HashSet. It writes the found lines in a temp file.

        if (File.Exists(@"temp.txt")) File.Delete(@"temp.txt");

        String line;
        String oldLine = "";
        using (var fs = File.OpenRead(largeFileName))
        using (var sr = new StreamReader(fs, Encoding.UTF8, true))
        {
            HashSet<String> hash = new HashSet<String>();
            hash.Add("house");
            using (var sw = new StreamWriter(@"temp.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    foreach (String str in hash)
                    {
                        if (oldLine.Contains(str))
                        {
                            sw.WriteLine(oldLine); 
                            // write the next line as well (optional)
                            sw.WriteLine(line + "\r\n");                                    
                        }
                    }
                    oldLine = line;
                }
            }
        }

Fast way of searching for a string in a text file, Notepad++ has 'find in files' option, which is located in the 'Search' menu.. This will allow you to search multiple files in a folder (you can specify the folder) and  Fastest Way to search for a string in a large text file (75 to 100mb). Visual Basic .NET Forums on Bytes.

Fastest way of finding files which contain exclusively some text , OK, a much faster way (keeping it as Java), is to do the following: Convert the search string ('are') to a byte-array in the same encoding as the file. Open a memory-mapped byte-buffer from a File-Channel on the file. Scan the ByteBuffer, looking for matches to the search byte-array. count the newlines as you go. If you have a bunch of text files in a directory hierarchy, e.g, the Apache configuration files in /etc/apache2/ and you want to find the file where a specific text is defined, then use the -r option of the grep command to do a recursive search. This will perform a recursive search operation trough files for the string "197.167.2.9" (as shown

Finding a File Containing a Particular Text String In Linux Server , Something like ps ax|grep ntpd|cut -b1-5|xargs -I PID find /var/run/ -type f -exec grep '-l' '^PID$' {} ';'. Note that the grep pattern specifies that  Performance Benchmark. In case you are wondering which one will perform better if the string is too big, ‘regex.test’ is the fastest, followed by ‘string.search’ from ES6 libraries, and the third place belongs to string.indexof: The following is the Url for the JavaScript benchmark if you are interested to play with it:

How to search strings (C# Guide), Finding a file containing a particular text string - Explains how to search for a word or string on Linux server using grep command line options. find (str, beg=0, end=len (string)): Determines whether str occurs in a string and outputs the index of the location. You can limit the search by specifying a beginning index using beg or a ending index using end. index (str, beg=0, end=len (string)): Provides the same functionality as find (), but raises an exception when str isn’t found.

Comments
  • 10 KB is really tiny. I've worked with files too big to fit in memory =/
  • line by line could occur error. e.g: "AC" "TION" could be on different lines (if the coder has not given any attention on text file) String.Contains is best function to run out.
  • @icaptan - yep. That's why I did the ReadAllText example first, in case the keywords were split across two or more lines.
  • If the match text is on two different lines then they will be separated by a new line character and it still wouldn't match. You'd have to strip out any line breaks first.