3

I'm trying to read the text files that are generated from another program and parse them to perform other actions.

This topic is close but the file I'm reading has more "structuring" for lack of a better word. Here is a sample of what the file I am reading will look like for reference:

data:extend(
{
{
type = "buildformula",
name = "item1",
active = false,
buildtime = 3,
ingredients =
{
  {"ingredient1", 1},
  {"ingredient2", 4},
  {"ingredient3", 5}
},
result = "finished item 1"
},        

{
type = "buildformula",
name = "item2",
active = true,
buildtime = 12,
ingredients =
{
  {"ingredient1", 2},
  {"ingredient2", 3}
},
result = "finished item 2"
},
}
)

One other suggestion in that post referenced ConvertFrom-String command which seems to have the power I need but i'm still getting no results.

Lastly here is the most recent code I have :

    $TemplateAdr = @'
{
type = "buildformula",
name = "item1",
active = false,
buildtime = 3,
ingredients =
{
  {"ingredient1", 1},
  {"ingredient2", 4},
  {"ingredient3", 5}
},
result = "finished item 1"
},        

{
type = "buildformula",
name = "item2",
active = true,
buildtime = 12,
ingredients =
{
  {"ingredient1", 2},
  {"ingredient2", 3}
},
result = "finished item 2"
},
'@
$output=Get-Content -Path "C:\temp\itembuilds.txt" |  ConvertFrom-String -TemplateContent $TemplateAdr

I'm new to PowerShell but I feel like this is possible.

Eleron
  • 33
  • 6
  • Please, can you give a example of itembuilds.txt. – JPBlanc Feb 13 '18 at 16:12
  • The first block is a copy paste from the file of which i'm attempting to read. – Eleron Feb 13 '18 at 16:21
  • Your data looks like it's [JSON](https://en.wikipedia.org/wiki/JSON#Example). I'm not too familiar with that, and trying to convert your sample using `ConvertFrom-Json` failed (`Invalid Object pass in`). Any luck using this command with the full file? Do you know if this is meant to be standard JSON? – G42 Feb 13 '18 at 16:37
  • @gms0ulman I typed `ConvertFrom-Json -InputObject C:\temp\itembuilds.txt` and got `ConvertFrom-Json : Invalid JSON primitive: C.` – Eleron Feb 13 '18 at 17:00
  • 1
    @gms0ulman Also thanks for turning me onto JSON. I had not known of that but after reviewing your link it sure looks like that's the intent of the file. I'll see if I can confirm that and report back. – Eleron Feb 13 '18 at 17:05
  • @Eleron Hope that leads you to a solution. The command you tried attempts to the the _string_ into json. Try: `Get-Content -Path "C:\temp\itembuilds.txt" | ConvertFrom-Json` – G42 Feb 13 '18 at 17:07
  • 1
    @gms0ulman I got `ConvertFrom-Json : Invalid JSON primitive: data. At line:1 char:46 + Get-Content -Path "C:\temp\itembuilds.txt" | ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand` – Eleron Feb 13 '18 at 17:34
  • In the non-sample version of the file there is one line at the top. I copied sample data when I thought I was dealing with structured strings and missed that line. Not sure if its relevant to the JSON discussion but I have updated my source with the line and its closing paren. Apologies on missing that! – Eleron Feb 13 '18 at 17:45
  • Well, the text that you have posted does not appear to be valid JSON anyway. – EBGreen Feb 13 '18 at 20:49

1 Answers1

1

Your $TemplateAdr template is not marked up as such.

Try this one:

$TemplateAdr = @'
data:extend(
\{
\{
type = "{Type*:buildformula}",
name = "{Name:item1}",
active = {[bool]Active:false},
buildtime = {[int]BuildTime:3},
{Ingredients:ingredients =
\{
  \{"{IngredientName*:ingredient1}", {[int]IngredientCount:1}\},
  \{"{IngredientName*:ingredient2}", {[int]IngredientCount:4}\},
  \{"{IngredientName*:ingredient3}", {[int]IngredientCount:5}\}
\},}
result = "{[string]Result:finished item 1}"
\},

\{
type = "{Type*:buildformula}",
name = "{Name:item2}",
active = {[bool]Active:true},
buildtime = {[int]BuildTime:12},
{Ingredients:ingredients =
\{
  \{"{IngredientName*:ingredient1}", {[int]IngredientCount:2}\},
  \{"{IngredientName*:ingredient2}", {[int]IngredientCount:3}\}
\},}
result = "{[string]Result:finished item 2}"
\},
\}
)
'@

Get-Content -Path "C:\temp\itembuilds.txt" |
    ConvertFrom-String -TemplateContent $TemplateAdr |
        Select-Object -Property Type,Name,Active,BuildTime,@{Name='Ingredients';Expression={$_.Ingredients.Items}}

Curly braces are escaped using \. I've explicitly specified PowerShell types for some of the properties (You might not want that). I've used Select-Object to map ingredients list to a new property.

Feel free to change property names inside field specifications ({PropertyName:whatever}).

Giorgi Chakhidze
  • 3,351
  • 3
  • 22
  • 19