简体   繁体   中英

SSIS Filtering records from flat file

I'm trying to filter records in flat file that conatin string "Order" and then import result to the excel file.

I used the Conditional Split component for this with condition FINDSTRING(MyColumn,"Order",1) > 0 but it does not work - no record is imported as result.

Rows in my flat file looks like this: 2020-01-01 11:51:00 3459 Order:1398 session:fr34skjdnn32kjsd ID:67889

Do you have any idea how to solve this? Thanks in advance for help!

The code below is only a sample of how to read the file. I would need to see more lines of the input to get code to work with any input. I've been parsing text files like this for over 45 years and have learned that ONE row of input is never sufficient to get code like this to work with ALL inputs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            Order order = new Order();
            List<Order> orders = order.ReadFile(FILENAME);
        }
    }
    public class Order
    {
        public DateTime date { get; set; }
        public string order { get; set; }
        public string session { get; set; }
        public string id { get; set; }

        public List<Order> ReadFile(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            List<Order> orders = new List<Order>();
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                Order newOrder = new Order();
                orders.Add(newOrder);
                string dateStr = line.Substring(0, 24);
                newOrder.date = DateTime.ParseExact(dateStr, "yyyy-MM-dd hh:mm:ss ffff", System.Globalization.CultureInfo.InvariantCulture);

                string pattern = @"(?'key'[^:]+):(?'value'[^\s]+)";
                MatchCollection matches = Regex.Matches(line.Substring(24), pattern);

                foreach (Match match in matches.Cast<Match>().AsEnumerable())
                {
                    string key = match.Groups["key"].Value.Trim();
                    string value = match.Groups["value"].Value.Trim();

                    switch (key)
                    {
                        case "Order" :
                            newOrder.order = value;
                            break;

                        case "session":
                            newOrder.session = value;
                            break;

                        case "ID" :
                            newOrder.id = value;
                            break;


                    }
                }
            }
            return orders;
        }
    }
}

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