简体   繁体   中英

Extract specific lines of text from a text file c#

I am reading in a text file line by line using StreamReader and displaying it to the console. It reads in and displays just fine. Each line contains from left to right, Date, Time, Number, Entry. The text file has hundreds of lines of text. I want to allow a user to enter a specific Date and Time and get back only the lines of text within that time frame. So, out of hundreds of lines of text, they can be returned only the lines of text for their inputted Date and Time.

Here's my code so far to read in and display the text file.

    public override void ReadFile(string strFileName)
    {
        try
        {
            using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
            {
                String line = sr.ReadLine();
                Console.WriteLine(line);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }

This works and displays text on the console like the following:

01-01-2015 10:10:10 Line of text blah blah blah
01-01-2015 10:10:10 Line of text blah blah blah
01-01-2015 10:10:10 Line of text blah blah blah

I am now trying to separate the lines to get the individual parts like date and time. This is what I have so far but I'm not sure what's the best way to proceed.

        public override void ReadLiFile(string strFileName)
    {
        try
        {
            using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] fields = line.Split("\t".ToCharArray()); 
                    int theInt = Convert.ToInt32(fields[0]);//to get first field e.g Date
                }
                Console.WriteLine(line);
            }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }

I want to narrow down what's output to screen based on a given date and time. I don't want all the lines to appear just the lines relevant to a certain date and time say for example lines between 01-01-2015 10:10:10 and 01-01-2015 10:15:15

I'm guessing I need to isolate the date and time fields in each line and store the value. I have tried to split the line and store the first value which is Date. My question is what's the best way to split the fields in the line? My attempt is not working. I'm now getting back an error saying the 'File cannot be read' and also 'Input string is not in the correct format' Thank you for any help.

This will extract the date portion and parse it to a DateTime , which you can then filter by.

var startDate = new DateTime(2015, 1, 1, 10, 10, 10);
var endDate = new DateTime(2015, 1, 1, 10, 15, 15);

var result = (from line in File.ReadLines(@"C:\MyFolder\TextFile.txt")
              let pieces = line.Split('\t')
              let date = DateTime.ParseExact(pieces[0] + pieces[1], "MM-dd-yyyyHH:mm:ss", CultureInfo.InvariantCulture)
              where date >= startDate && date <= endDate
              select line).ToList();

This is LINQ, in case you haven't seen it before. Once you get used to it, it's great for querying.

The let keyword allows you to store data, so that (for example) you can use it multiple times throughout the query without needing to perform ParseExact or a Split multiple times.

File.ReadLines reads as few lines from the file as possible, just enough to satisfy your query. Though in this case, where it has to parse each line to check the date, it's reading them all anyway.

DateTime dt;
if (DateTime.TryParse(fields[0] + " " + fields[1], out dt))
    if (dt >= minDate && dt <= maxDate)

This should print all lines with Dates between startDate and endDate . I have omitted the time part, but you should get an idea of how to do it.

DateTime startDate = new DateTime(2015, 1, 1);
DateTime endDate = new DateTime(2015, 1, 3);
var query = File.ReadLines(@"e:\1.txt").Select(line => new { Line = line, Date = Convert.ToDateTime(line.Split(' ').First()) })
                                        .Where(x => x.Date >= startDate && x.Date <= endDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM