首页 > 解决方案 > Updating a Sub-component in a Variable in Powershell

问题描述

I am trying to update data inside a variable with Powershell. I can retrieve the data I need out of the variable but I am unable to update only a single field inside the variable.

I can update an entire line by using $HL7data[0] = 'foo' but I don't know how to change just a single field within a line, i.e. when delimited by | and ^.

The main issue as I see it is that it becomes string data and I am no longer working inside a variable.

I have tried find and replace on the string I output. This has worked for unique values, but values such as suburbs might exist several times in the data and all get replaced.

I have also tried splitting the data into a multidimensional array, but I don't know to store the result as an array and split it further, and I also don't know how to join all the data again as each layer uses a different delimiter.

Ideally, I'd like to drill down into the data by delimiter (newline, |, ^, etc.) and position, change the value retrieved, and save it back into the original structure.

This is the code I've tried:

$HL7data = get-content("5a0a7726672afLOE_1802.hl7")

(((($HL7data[0] -split("\|"))[11])-split("\^"))[2]) = 'Not Jingili"

This is the test data (inside 5a0a7726672afLOE_1802.hl7) which is two lines:

PID|||^^^AUSHIC^MC||Lname^Fname^^^Mr||||||123 Test Street^^Jingili^NT^0881||^PRN^PH^^^^|^WPN^PH^^^^|
OBR||||GRF^FileName||20171114124758|

The suburb Jingili should change to Not Jingili in the complete data. The code I've tried does not change the contents of the variable $HL7data.

I've also just tried the following code to break down the steps:

$ary1 = @()
foreach ($line in $HL7data)
{
    $ary2 = @()
    foreach ($Seg in $line-split("\|"))
    {
        $ary3 = @()
        foreach ($item in $seg-split("\^"))
        {
            $ary3 += $item
        }
        $ary2 += $ary3
    }
    $ary1 += $ary2
}
$ary1[0][11][2] = "Not Jingili"

The last line $ary1[0][11][2] = "Not Jingili" doesn't work. It gives an error:

Cannot index into a null array.
At line:19 char:1
+ $ary1[0][11][2] = "Not Jingili"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

If it had worked, I would have then recombined the nested arrays back into a text data structure.

标签: powershellvariablessplit

解决方案


# Read data from file
$file = Get-Content "5a0a7726672afLOE_1802.hl7"

# Split data into a multidimensional array
$segments = @()
foreach ($line in $file)
{
  $fields = @()
  foreach ($field in $line -split "\|")
  {
    $fields += ,@($field -split "\^")
  }
  $segments += ,@($fields)
}

# Change as many values as you want here
$segments[0][11][0] = "1 Test Ave"
$segments[0][11][2] = "Not Jingili"

# Join data into a single flat array
$flat_segments = @()
foreach ($fields in $segments)
{
  $flat_fields = @()
  foreach ($field in $fields)
  {
    $flat_fields += $field -join "^"
  }
  $flat_segments += $flat_fields -join "|"
}

# Dump changed data
$flat_segments

Note: I see you are working with HL7 data. Each line in the new file should end with \r (0x0D), not \n (0x0A) or \r\n (0x0D,0x0A). Powershell doesn't care what line endings the input file is using and will discard them.


推荐阅读