Home > Software engineering >  How to read the text file using LINQ with specified line number
How to read the text file using LINQ with specified line number

Time:07-01

I have a text file which is mentioned below, I need to find the total count of line numbers which starts with "3" and the total count is already available in the file which is available in the position line starts with "7200" - Position starts with 05 and length is 6. Similar way. Total amount also available in the line starts with "7200" - Position starts with 21 and length is 12.

console output

EDIT

You have asked a great question about the performance. The great thing is that we don't have to speculate or guess! There is always a way to measure performance.

Here's the benchmark I just put together. And look, I did it really quickly so if anyone spots something I missed please point it out. But here's what I get:

static void Main(string[] args)
{
    var path = Path.Combine(
        Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
        "TextFile.txt");
    try
    {
        // 200K lines of random guids
        List<string> builder = 
            Enumerable.Range(0, 200000)
            .Select(n => $"{{{System.Guid.NewGuid().ToString()}}}")
            .ToList();

        var footer =
            File.ReadAllLines(path);

        builder.AddRange(footer);

        var FileLines = builder.ToArray();

        var benchmark = new System.Diagnostics.Stopwatch();
        benchmark.Start();
        int totalCount = int.MinValue;
        foreach (var line in FileLines)
        {
            //// If line length is zero, then do nothing
            if (line.Length == 0)
            {
                continue;
            }
            // Original code from post
            // switch (line.Substring(1, 1))
            // Should be:
            switch (line.Substring(0, 1))
            {
                case "7":
                    totalCount = int.Parse(line.Substring(4, 6));
                    // This is another issue!! Breaking from the switch DOESN'T break from the loop
                    break;
                    // SHOULD BE: goto breakFromInner;
                    // One of the few good reasons to use a goto statement!!
            }
        }
        benchmark.Stop();
        Console.WriteLine($"200K lines using Original code: Elapsed = {benchmark.Elapsed}");
        Console.WriteLine($"Count = {totalCount}");


        benchmark.Restart();
        for (int i = FileLines.Length - 1; i >= 0; i--)
        {
            var line = FileLines[i];
            //// If line length is zero, then do nothing
            if (line.Length == 0)
            {
                continue;
            }
            // Original code from post
            // switch (line.Substring(1, 1))
            // Should be:
            switch (line.Substring(0, 1))
            {
                case "7":
                    totalCount = int.Parse(line.Substring(4, 6));
                    // One of the few good reasons to use a goto statement!!
                    goto breakFromInner;
            }
        }
        // See note
        breakFromInner:
        benchmark.Stop();
        Console.WriteLine($"200K lines using Original code with reverse: Elapsed = {benchmark.Elapsed}");
        Console.WriteLine($"Count = {totalCount}");

        benchmark.Restart();
        var count =
            int.Parse(
                FileLines
                .Reverse()
                .First(line => line.Any() && (line.First() == '7'))
                .Substring(4, 6));
        benchmark.Stop();
        Console.WriteLine($"200K lines using Linq with Reverse: Elapsed = {benchmark.Elapsed}");
        Console.WriteLine($"Count = {count}");
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Assert(false, ex.Message);
    }
}

console output

  • Related