1

I've been breaking my head over this for a while now. I run two pipelines to produce two arrays of tuples. Each tuple contains a (filename, property).

# create pipeline of (file, property1) tuples
$prop1 = Get-ChildItem *.prop1 -PipelineVariable fi |
           Get-Content -Tail 150 |
           Select-String -Pattern "  Pattern1 (\d+)" | 
           ForEach-Object { $_.Matches } |
           ForEach-Object { [System.Tuple]::Create($fi.Name, $_.Groups[1].Value) }

# create pipeline of (file, property2) tuples
$prop2 = Get-ChildItem *.prop2 -PipelineVariable fi | 
           Get-Content -Tail 5 | 
           Select-String -Pattern "  Pattern2 (\d+)" | % { $_.Matches } |
           ForEach-Object { $_.Matches } | 
           ForEach-Object { [System.Tuple]::Create($fi.Name, $_.Groups[1].Value) }

$result = ????
ConvertTo-Json $result

Now I would like to combine these tuple pipelines on filename to produce JSON output like:

[
  {
     'file':'filename1',
     'prop1':'value1',
     'prop2':'value2',
  },
  {
     'file':'filename2',
     'prop1':'value3',
     'prop2':'value3',
  }
]

How can I combine these pipelines to produce the desired output? Or am I making this more complicated than necessary?

2
  • And you are sure that there's always the exact same number of tuples in $prop1 and $prop2? Commented Jun 13, 2016 at 22:42
  • @MathiasR.Jessen yes Commented Jun 13, 2016 at 23:47

1 Answer 1

2

Instead of binding the property values to the filename with a Tuple, use a Hashtable where the filename is the key:

$FileProps = @{}
Get-ChildItem *.prop1 -PipelineVariable fi |
           Get-Content -Tail 150 |
           Select-String -Pattern "  Pattern1 (\d+)" | 
           ForEach-Object { $_.Matches } |
           ForEach-Object { $FileProps[$fi.BaseName] = @{ 'filename' = $fi.BaseName; 'prop1' = $_.Groups[1].Value }}

Get-ChildItem *.prop2 -PipelineVariable fi | 
           Get-Content -Tail 5 | 
           Select-String -Pattern "  Pattern2 (\d+)" | % { $_.Matches } |
           ForEach-Object { $_.Matches } | 
           ForEach-Object { $FileProps[$fi.BaseName]['prop2'] = $_.Groups[1].Value }

Now create a bunch of custom objects based on the hashtable entries and convert them to json:

$Json = $FileProps.Keys |ForEach-Object {
    New-Object psobject -Property $FileProps[$_]
} |ConvertTo-Json
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works! I could not get the nested hashtable right.

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.