C# Programming How to grep columns/lines from Text File?

12,201

Solution 1

Well, as a quick fix for the specific example:

if (line.StartsWith("Sun Nov 19 2000"))
{
    Console.WriteLine(line);
}

You could use Contains to find a substring within the line.

Note that loading the whole file in an array won't scale well for very large logs. We can look into fixing that if it's an issue for you - but let's take things slowly :)

Solution 2

You could do this using Regex to select matching lines in a richer way than string.Contains allows.

Not sure why you are reinventing findstr.exe though.

For large files you might find File.ReadLines (.Net 4 only) performs better - this reads the same lines but allows you to process them in a foreach and other IEnumerable scenarios without loading the entire file into RAM at once.

Solution 3

Here's a grep style method I use in testing:

    public static List<string> FileGrep(string filePath, string searchText)
    {
        var matches = new List<string>();

        using (var f = File.OpenRead(filePath))
        {
            var s = new StreamReader(f);

            while (!s.EndOfStream)
            {
                var line = s.ReadLine();

                if (line != null && line.Contains(searchText)) matches.Add(line);   
            }

            f.Close();
        }

        return matches;
    }
Share:
12,201
JavaNoob
Author by

JavaNoob

Updated on June 09, 2022

Comments

  • JavaNoob
    JavaNoob about 2 years

    I have a C# console program which main functions should let a user grep lines / columns from a log text file.

    An Example within the text file the user wishes to grep a group of all the related lines starting from a particular date etc. "Tue Aug 03 2004 22:58:34" to "Wed Aug 04 2004 00:56:48". Therefore after processing, the program would then output all the data found within the log text files between the 2 dates.

    Could someone please advise on some codes that I could use to grep or create a filter to retrieve the neccessary text/data from the file? Thanks!

    C# Program Files:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.IO;
    
    namespace Testing
    {
    class Analysis
    {
        static void Main()
        {
            // Read the file lines into a string array.
            string[] lines = System.IO.File.ReadAllLines(@"C:\Test\ntfs.txt");
    
            System.Console.WriteLine("Analyzing ntfs.txt:");
    
            foreach (string line in lines)
            {
                Console.WriteLine("\t" + line);
    
                //  ***Trying to filter/grep out dates, file size, etc****
                if (lines = "Sun Nov 19 2000")
                {
                    Console.WriteLine("Print entire line");
                }
            }
    
            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    }
    

    Log Text File Example:

    Wed Jul 21 2004 16:58:48   499712 m... r/rrwxrwxrwx 0        0        8360-128-3 
    C:/Program Files/AccessData/Common Files/AccessData LicenseManager/LicenseManager.exe
    
    Tue Aug 03 2004 22:58:34    23040 m... r/rrwxrwxrwx 0        0        8522-128-3 
    C:/System Volume Information/_restore{88D7369F-4F7E-44D4-8CD1-
    F7FF1F6AC067}/RP4/A0002101.sys
    
    23040 m... r/rrwxrwxrwx 0        0        9132-128-3 
    C:/WINDOWS/system32/ReinstallBackups/0003/DriverFiles/i386/mouclass.sys
    
    23040 m... r/rrwxrwxrwx 0        0        9135-128-4 C:/System Volume 
    Information/_restore{88D7369F-4F7E-44D4-8CD1-F7FF1F6AC067}/RP4/A0003123.sys
    
    23040 m... r/rrwxrwxrwx 0        0        9136-128-3 
    C:/WINDOWS/system32/drivers/mouclass.sys
    
    Tue Aug 03 2004 23:01:16   196864 m... r/rrwxrwxrwx 0        0        4706-128-3 
    C:/WINDOWS/system32/drivers/rdpdr.sys
    
    Tue Aug 03 2004 23:08:18    24960 m... r/rrwxrwxrwx 0        0        8690-128-3 
    C:/WINDOWS/system32/drivers/hidparse.sys