1

I want to parse the following text file into PowerShell objects:

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

The OBJECT-PROPERTIES should be properties of the PowerShell objects. I would also like the text of the object to be included in the object.

I was stating to make some regular expression, but I don't know how to parse all that information into one regular expression.

My object is far longer than 2 objects, but just for the examples sake, its only 2 objects.

Expected Output:

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 Answers 2

1

I managed to do following:

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

Output:

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=;
                 }
               }
Sign up to request clarification or add additional context in comments.

Comments

1

Here is an example whith one regex to capture all properties except the Object itself (you may need a new one for that or work with nested capturing groups):

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

Output:

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 : 

Regex:

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

Regular expression visualization

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.