2016-09-09 32 views
1

我想下面的文本文件解析爲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>" 

回答

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

這裏是蒙山一個正則表達式的例子來捕捉除了物體本身的所有屬性(你可能需要建立一個新的或嵌套捕獲組工作):

$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

相關問題