Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file

grep

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
like image 983
JavaNoob Avatar asked Dec 21 '22 21:12

JavaNoob


1 Answers

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.

like image 63
Steve Townsend Avatar answered Dec 24 '22 09:12

Steve Townsend