将文本文件解析为多个PowerShell对象

时间:2016-09-09 09:59:15

标签: regex powershell

我想将以下文本文件解析为PowerShell对象:

OBJECT Table 60000 My table 1
{
  OBJECT-PROPERTIES
  {
    Date=09-09-16;
    Time=11:27:31;
    Modified=Yes;
    Version List=;
  }
}

OBJECT Page 60001 My Page
{
  OBJECT-PROPERTIES
  {
    Date=09-09-16;
    Time=11:28:18;
    Modified=Yes;
    Version List=;
  }
}

OBJECT-PROPERTIES应该是PowerShell对象的属性。我还希望将对象的文本包含在对象中。

我说要做一些正则表达式,但我不知道如何将所有信息解析成一个正则表达式。

我的对象远远超过2个对象,但仅为了示例,它只有2个对象。

预期输出:

Object1:
  Type: Table
  Number: 60000
  Name: "My table 1"
  Date: "09-09-16"
  Time: "11:28:18"
  Modified: "Yes"
  "Version List": ""
  Object: "<All of the text in this object>"

Object2:
  Type: Page
  Number: 60001
  Name: "My Page"
  Date: "09-09-16"
  Time: "11:28:18"
  Modified: "Yes"
  "Version List": ""
  Object: "<All of the text in this object>"

2 个答案:

答案 0 :(得分:1)

这是一个示例,其中一个正则表达式捕获除Object本身之外的所有属性(您可能需要一个新的属性或使用嵌套捕获组):

$regex = 'OBJECT\s+(?<type>\w+)\s+(?<number>\d+)\s+(?<name>.+?)\s{.*?Date=(?<date>[^;]+).*?Time=(?<time>.+?);.*?Modified=(?<modified>.+?);.*?Version List=(?<versionlist>.*?);'
$content = Get-Content $scripts.tmp
$matches = [regex]::Matches($content, $regex)
$matches | ForEach-Object {
    [PSCustomObject]@{
        Type = $_.Groups['type'].Value
        Number = $_.Groups['number'].Value
        Name = $_.Groups['name'].Value
        Date = $_.Groups['date'].Value
        Time = $_.Groups['time'].Value
        Modified = $_.Groups['modified'].Value
        "Version List" = $_.Groups['versionlist'].Value
    }
}

<强>输出:

Type         : Table
Number       : 60000
Name         : My table 1
Date         : 09-09-16
Time         : 11:27:31
Modified     : Yes
Version List : 

Type         : Page
Number       : 60001
Name         : My Page
Date         : 09-09-16
Time         : 11:28:18
Modified     : Yes
Version List : 

<强>正则表达式:

OBJECT\s+(?<type>\w+)\s+(?<number>\d+)\s+(?<name>.+?)\s{.*?Date=(?<date>[^;]+).*?Time=(?<time>.+?);.*?Modified=(?<modified>.+?);.*?Version List=(?<versionlist>.*?);

Regular expression visualization

答案 1 :(得分:1)

我设法做了以下事情:

$txt = "OBJECT Table 60000 My table 1
{
  OBJECT-PROPERTIES
  {
    Date=09-09-16;
    Time=11:27:31;
    Modified=Yes;
    Version List=;
  }
}

OBJECT Page 60001 My Page
{
  OBJECT-PROPERTIES
  {
    Date=09-09-16;
    Time=11:28:18;
    Modified=Yes;
    Version List=;
  }
}"
$expr = "(?<Object>OBJECT (?<Type>\w+) (?<Number>\d+) (?<Name>[\w ]+)\s*{\s*OBJECT-PROPERTIES\s*{\s*Date=(?<Date>[\d-]+);\s*Time=(?<Time>[\d:]+);\s*Modified=(?<Modified>\w+);\s*Version List=(?<Version>[^;]*);\s*}\s*})"
[Regex]::Matches($txt, $expr) | % {
    [PSCustomObject]@{
      Type = $_.Groups["Type"].Value;
      Number = $_.Groups["Number"].Value;
      Name = $_.Groups["Name"].Value;
      Date = $_.Groups["Date"].Value;
      Time = $_.Groups["Time"].Value;
      Modified = $_.Groups["Modified"].Value;
      "Version List" = $_.Groups["Version"].Value;
      Object = $_.Groups["Object"].Value
    }
}

<强>输出:

Type         : Table
Number       : 60000
Name         : My table 1
Date         : 09-09-16
Time         : 11:27:31
Modified     : Yes
Version List : 
Object       : OBJECT Table 60000 My table 1
               {
                 OBJECT-PROPERTIES
                 {
                   Date=09-09-16;
                   Time=11:27:31;
                   Modified=Yes;
                   Version List=;
                 }
               }

Type         : Page
Number       : 60001
Name         : My Page
Date         : 09-09-16
Time         : 11:28:18
Modified     : Yes
Version List : 
Object       : OBJECT Page 60001 My Page
               {
                 OBJECT-PROPERTIES
                 {
                   Date=09-09-16;
                   Time=11:28:18;
                   Modified=Yes;
                   Version List=;
                 }
               }