简体   繁体   English

SSIS 从平面文件过滤记录

[英]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.我正在尝试过滤包含字符串“Order”的平面文件中的记录,然后将结果导入到 excel 文件中。

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.我为此使用了条件拆分组件,条件 FINDSTRING(MyColumn,"Order",1) > 0 但它不起作用 - 结果没有导入任何记录。

Rows in my flat file looks like this: 2020-01-01 11:51:00 3459 Order:1398 session:fr34skjdnn32kjsd ID:67889我的平面文件中的行如下所示: 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我已经解析这样的文本文件超过 45 年了,并且了解到一行输入永远不足以让这样的代码处理所有输入

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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