通过在字符串中找到一行加上下一行来解析文件

时间:2017-12-03 11:12:08

标签: c#

我从应用程序获得以下输出并尝试使用c#asp.net

解析它

输出位于link

------------+-------------------------
ID          |3013940039
Session Name|SID-0000000058901C19-440
IP Address  |10.10.0.10 (DHCP)
Created at  |2017-12-03 09:37:37
Updated at  |2017-12-03 10:24:34
Location    |On 'Agmvpn01'
------------+-------------------------
ID          |3440042004
Session Name|SID-0000000061A459D0-443
IP Address  |10.10.0.12 (DHCP)
Created at  |2017-12-03 09:39:11
Updated at  |2017-12-03 10:24:35
Location    |On 'Agmvpn01'
------------+-------------------------
ID          |498444455
Session Name|SID-000000009EA543D1-442
IP Address  |10.10.0.13 (DHCP)
Created at  |2017-12-03 09:48:37
Updated at  |2017-12-03 10:24:35
Location    |On 'Agmvpn01'
------------+-------------------------
ID          |1631934168
Session Name|SID-000000002C393E76-432
IP Address  |10.10.0.14 (DHCP)
Created at  |2017-12-03 09:34:44
Updated at  |2017-12-03 10:24:35
Location    |On 'Agmvpn01'
------------+-------------------------
ID          |3771126469
Session Name|SID-0000000061A459D0-443
IP Address  |169.254.234.70
Created at  |2017-12-03 06:39:05
Updated at  |2017-12-03 06:39:09
Location    |On 'Agmvpn01'

我想找到DHCP分配的IP地址加上下一行"创建于"

所以最后我想结束

IP Address  |10.10.0.10 (DHCP)
Created at  |2017-12-03 09:37:37

目前我正在使用以下内容获取IP地址,但我现在也在下一行之后

 List<string> foundDevicesIP = lines.ToList().Where(x =>
                    x.Contains("DHCP")).ToList();
 //sometimes DHCP flag is missing might need to grep for something else

 for (int i = 0; i < foundDevicesIP.Count; i = i + 1)
 {
     string[] ip2 = foundDevicesIP[i].Split('|');
     string[] finalIP = ip2[1].Split('(');
     lstAirMasjidFound.Items.Add(finalIP[0].TrimEnd());
 }

1 个答案:

答案 0 :(得分:1)

如果您想使用LINQ查询执行此操作,则可以分两步执行此操作,首先获取彼此属于的行的集合。在你的例子中,由“-------- + --------”行表示。我使用了一个runningSection变量。在同一个查询中,我们选择了我们感兴趣的行。在您的情况下, DHCP 创建。
由此我们可以进行分组,仅选择至少有两个结果的组。剩下的就是在适当的字段中选择正确的行。

这就是你的代码:

var runningsection = 0;
const string dhcp = "(DHCP)";
var foundDevicesIP = from l in lines
    let section = l.StartsWith("----") ? 
                  runningsection++:
                  runningsection                             // grouping for sections
    where l.StartsWith("Created at") || l.Contains(dhcp)     // which lines to keep
    group new {                                              // type with line and linetype properties
        line =  l.Split('|')[1].Replace(dhcp,String.Empty),  // parse/shape the value
        linetype = l.Contains(dhcp)?1:2                      // was it an IP address (1) or Created at (2)
    } by section into agg                                    // group the lines beloning to the same section
    where agg.Count() > 1                                    // which groups have 2 or more lines
    select new
        {   
            IP = agg.First( l => l.linetype == 1).line,      // the first object with linetype 1 is the IP
            Created = agg.First( l => l.linetype == 2).line  // the first object with linetype 2 is the creation date
        };


    var list = new List<string>();
    foreach(var deviceIP in foundDevicesIP) 
    {
        list.Add(String.Format("IP: {0} , # {1} #", deviceIP.IP, deviceIP.Created));
    }

使用您的示例输入运行时,它将位于列表中:

list of IP and created