如何解析这种字符串?

时间:2018-10-14 12:41:51

标签: c#

我得到以下格式的输出

                            size client                                 ssflags                                 group                                   flags                                                 date     time                                                level volume                                               ss completed              name
                       290502556 awsknwpi00                             vrF                                     BOOTSTRAP PROTECTION                    cr                                                        10/05/2018 07:56:29 PM                                    full AFTD-Bootstrap-00                                        10/05/2018 07:56:36 PM bootstrap  
                       916940228 aunnd.production.transfservices.com vF                               AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:07:28 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:17:52 PM C:\
                            5016 aunnd.production.transfservices.com vF                               AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:07:34 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:07:51 PM F:\
                       579984280 aunnd.production.transfservices.com vF                               AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:09:24 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:17:48 PM VSS SYSTEM FILESET:\
                        35291616 aunnd.production.transfservices.com vF                               AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:53 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:09:30 PM VSS SYSTEM BOOT:\
                       638349452 aunnd.production.transfservices.com vF                               AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:09:20 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:13:03 PM VSS SYSTEM FILESET:\
                      2774969836 aunnd.production.transfservices.com vF                             AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:11 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:30:45 PM D:\
                       743703956 aunnd.production.transfservices.com vF                             AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:10 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:30:54 PM E:\
                       410140044 aunnd.production.transfservices.com vF                             AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:09 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:45 PM F:\
                        71691676 aunnd.production.transfservices.com vF                              GIS Servers_Dup                         cb                                                        10/05/2018 01:00:08 AM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 01:00:48 AM VSS SYSTEM SERVICES:\
                        15075028 aunnd.production.transfservices.com vrF                            AWS-FS-BKP-DUPL-TEMP                    cr                                                        10/05/2018 07:17:24 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:17:53 PM \\?\VOLUME{2186CF2E-5D39-11DF-9211-806E6F6E6963}\
                         3562216 aunswpfilv03.production.transfieldservices.com vF                              GIS Servers_Dup                         cb                                                        10/05/2018 01:01:05 AM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 01:06:05 AM D:\
                      1382628028 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:07:17 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:38 PM C:\
                               4 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:07:18 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:07:37 PM P:\
                       258436476 aunnd.production.transfservices.com vrF                               AWS-FS-BKP-DUPL-TEMP                    cr                                                        10/05/2018 07:07:16 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:07:55 PM \\?\VOLUME{CE3268A4-79C3-11E6-8138-806E6F6E6963}\
                               4 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:07:19 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:07:38 PM D:\
                           21956 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:07:17 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:07:33 PM E:\
                         3922628 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:11:29 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:48 PM WINDOWS ROLES AND FEATURES:\
                       258436476 aunnd.production.transfservices.com vrF                               AWS-FS-BKP-DUPL-TEMP                    cr                                                        10/05/2018 07:07:18 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:08:05 PM \\?\VOLUME{15D9292D-7A06-11E6-8138-806E6F6E6963}\
                       258436476 aunnd.production.transfservices.com vrF                               AWS-FS-BKP-DUPL                         cr                                                        10/05/2018 07:07:40 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:08:35 PM \\?\VOLUME{15D9292D-7A06-11E6-8138-806E6F6E6963}\
                      1153400396 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:09:09 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:30:18 PM C:\
                      1321796176 aunnd.production.transfservices.com vF                                AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:09:16 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:35:19 PM C:\
                        15075028 aunnd.production.transfservices.com vrF                               AWS-FS-BKP-DUPL-TEMP                    cr                                                        10/05/2018 07:09:15 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:09:46 PM \\?\VOLUME{AEF6A4F6-4DC8-11E0-92F6-806E6F6E6963}\
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:10:19 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:21 PM /bin
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:10:36 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:44 PM /boot
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:10:27 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:30 PM /cgroup
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:25 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:28 PM /dev
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:12 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:15 PM /etc
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:13 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:17 PM /lib64
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:10:48 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:57 PM /lost+found
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:11:06 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:18 PM /media
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:47 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:51 PM /media
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:57 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:02 PM /mnt
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:10:41 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:11:46 PM /net
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:27 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:13:31 PM /net
                         1707740 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:29 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:35 PM /nsr/cache
                           78220 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:09 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:09:12 PM /nsr/logs
                           84084 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:34 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:39 PM /nsr/logs
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:23 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:30 PM /nsr/lost+found
                       107087992 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:09:39 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:10:49 PM /nsr/nmc/nmcdb
                        87359588 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:12:48 PM                                    full AU-AWS-FS-BKP-001                                        10/05/2018 07:13:56 PM /nsr/nmc/nmcdb_stage
                            3360 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL-TEMP                    cb                                                        10/05/2018 07:08:08 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:09:12 PM /nsr/tmp
                            3360 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:39 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:44 PM /nsr/tmp
                               4 awsn.ad.broadrum.com        vF                                      AWS-FS-BKP-DUPL                         cb                                                        10/05/2018 07:11:18 PM                                    incr AU-AWS-FS-BKP-001                                        10/05/2018 07:12:22 PM /root

我正在尝试使用split方法解析输出。我尝试使用空间拆分,但它没有提供正确的输出。
我尝试了以下代码进行拆分:

string[] stringSeparators = new string[] { "\r\n" };
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray();
foreach (var item in lines)
{
    formatedop = Regex.Split(item, @"\s{3,}");
    //formatedop = item.Split('\t');
    formatedop = formatedop.Where(x => !string.IsNullOrEmpty(x)).ToArray();
}

我希望以以下格式输出:

formatedop[0]=290502556
formatedop[1]=awsnwkip01
formatedop[1]=vrF
formatedop[2]=BOOTSTRAP PROTECTION
formatedop[3]=cr
formatedop[4]=10/05/2018 07:56:29 PM
formatedop[5]=full
formatedop[6]=AFTD-Bootstrap-00
formatedop[7]=10/05/2018 07:56:36 PM
formatedop[8]=bootstrap

4 个答案:

答案 0 :(得分:1)

这看起来像是固定宽度的列,但是某些字段较大,这会引起问题。我能够使用正则表达式进行解析。参见下面的代码

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)
        {
            StreamReader reader = new StreamReader(FILENAME);
            string line = "";
            List<Client> clients = new List<Client>();
            int lineNumber = 0;
            while((line = reader.ReadLine()) != null)
            {
                if (++lineNumber > 1)
                {
                    clients.Add(new Client(line));
                }
            }


        }
    }
    public class Client
    {
        public long size { get; set; }
        public string client { get; set; }
        public string ssflags { get; set; }
        public string group { get; set; }
        public string flags { get; set; }
        public DateTime date { get; set; }
        public string level { get; set; }
        public string volume { get; set; }
        public DateTime completed { get; set; }
        public string name { get; set; }

        public Client(string line)
        {
            string pattern = @"^\s*(?'size'[^\s]+)\s+(?'client'[^\s]+)\s+(?'ssflags'[^\s]+)\s+" +
                @"(?'group'.*)\s+(?'flags'cr|cb)\s+(?'date'.+(AM|PM))\s+" +
                @"(?'level'[^\s]+)\s+(?'volume'[^\s]+)\s+" +
                @"(?'completed'.+(AM|PM))\s+(?'name'.*)";

            Match match = Regex.Match(line, pattern);

            size = long.Parse(match.Groups["size"].Value);
            client = match.Groups["client"].Value;
            ssflags = match.Groups["ssflags"].Value;
            group = match.Groups["group"].Value.Trim();
            flags = match.Groups["flags"].Value;

            date = DateTime.Parse(match.Groups["date"].Value);
            level = match.Groups["level"].Value;
            volume = match.Groups["volume"].Value;
            completed = DateTime.Parse(match.Groups["completed"].Value);
            name = match.Groups["name"].Value;

        }
    }
}

答案 1 :(得分:0)

由于您的字符串中要保留单个空格字符(例如“ 10/05/2018 07:56:36 PM”),但在其他地方,用作分隔符的单个空格字符(例如“ 4 awsn .ad.broadrum.com”),一种快速简便的方法是像这样(注意字符串分隔符中的两个空格字符!):

var result = line[1].Split(new string[]{"  "}, StringSplitOptions.RemoveEmptyEntries).ToString();

这会给你这个:

列表(5){“ 290502556 awsknwpi00 vrF”,“自举保护”,“ cr”,“ 2018/10/5 07:56:29 PM AFTD-Bootstrap-00”,“ 2018/10/05 07” :56:36 PM引导程序“}

然后,您可以将第一个项目进一步按单个空格分隔,并在索引1处插入该操作的输出,并在索引0 s.th处删除未拆分的版本。像这样:

result.InsertRange(1, result[0].Split(' '));
result.RemoveAt(0);

这将给你像这样:

列表(8){“”,“ 290502556”,“ awsknwpi00”,“ vrF”,“自举保护”,“ cr”,“ 2018年10月5日07:56:29 PM完整AFTD-引导程序-00 “,“ 2018年10月5日下午07:56:36引导程序”}

如您所见,您可能必须遍历并进行一些其他的修整,但这只是一个开始。

答案 2 :(得分:0)

只需使用此:

foreach (var item in lines)
{
    formatedop = imem.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
}

答案 3 :(得分:0)

我过去曾经使用过一个名为FileHelpers(https://www.filehelpers.net)的好的库。 它允许您解析由已知符号分隔的文件和具有固定长度字段的文件。我无法确定后者是否属于您的情况(从您粘贴的内容来看,我认为不是,但这可能是复制/粘贴问题) 还有另一个功能称为智能格式检测器(在此说明:https://www.filehelpers.net/example/Advanced/SmartFormatDetector/),您可以尝试查看它是否可以解决问题。 该库非常易于使用,我建议您尝试一下。