i got my xml file wehre i wanna sort out some Numbers.
{
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4062.198974609375,
201.51414489746095,
3538.89501953125
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
},
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4100.68408203125,
200.10733032226563,
3418.51171875
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
},
{
"Type": "bldr_chair",
"DisplayName": "bldr_chair",
"Position": [
4448.24755859375,
201.4647979736328,
3362.69970703125
],
"Orientation": [
0.0,
-0.0,
-0.0
],
"Scale": 1.0,
"Flags": 2147483647
}
But i need only
[4062.19897,201.51414,3538.89501],
Thats actually what i have. I try to sort my number or build up a array but its not easy in powershell to delcaire all benefits what need to hit.
$input = Get-Content $path
$input | Select-String -pattern '\d \.\d{5}' -AllMatches | %{$_.matches} | %{$_.value
} | Add-Content $Ausgabepfad
i hope you get some tips for me and i appreaciate your help
CodePudding user response:
Your string is a JSON not an XML as Mathias pointed out, you don't need to use regex to parse it.
$string = @'
"Position": [
4062.198974609375,
201.51414489746095,
3538.89501953125
],
'@
$json = "{ $string }" | ConvertFrom-Json
$json.Position | ConvertTo-Json -Compress
[4062.198974609375,201.51414489746094,3538.89501953125]
$path
path is likely a valid Json and probably you don't need to add the curly braces {...}
, in that case you would only do:
(Get-Content $path | ConvertFrom-Json).Position | ConvertTo-Json -Compress
I believe you're looking for something like this:
$json.EditorObjects.ForEach({
$coords = foreach($coord in $_.Position) {
[math]::Round($coord, 3, 'AwayFromZero')
}
,$coords
}) | Select-Object -First 5 | ConvertTo-Json -Compress
Which results in:
[[16112.0,578.507,4247.999],[5589.42,196.243,1670.624],[5448.696,193.072,2159.627],[5098.765,196.585,2797.101],[5418.692,197.421,2673.856]]
This invariably will be displayed a single line string, if you don't need this remove -Compress
.