3

I am processing a CSV file, and in one of the columns are cells that have the a string in an array format. Here is what accessing those cells looks like:

$csv = Import-Csv $filelocation
foreach ($line in $csv)
{
   Write-Host $line.ColumnName
}

Output:

[Property=[value1,value2,value3]]
[Property=[value1,value2]]
...

You can see that each cell outputs a string with an array structure. I want to treat each individual string as an array with Property[0] = value1, etc.

Is there a simple way to do this? Otherwise, I assume I will need to use Reg Ex.

8
  • 1
    The sample output you've provided seems to imply that the property values are strings. If so then if the string contains commas then it's going to be difficult with Regex. Can you provide the actual contents of the CSV file? Commented Jun 20, 2017 at 2:03
  • I cannot share the whole file, I think it has sensitive data, but here is a partial snippet from it using a raw text editor: ,,,,,"[AsymmetricKey=[]]","[AppAddress=[[AddressType=Reply,Address=urn:ietf:wg:oauth:2.0:oob]]]","[AppAuxiliaryId=[]]",,,, Here you can see a bunch of empty columns and a few of these "array like" strings. Commented Jun 20, 2017 at 2:11
  • What happens if a value of a property contains a comma? Commented Jun 20, 2017 at 2:13
  • I would hope to be able to navigate like a array of dictionaries. Something like: foreach($value in $line.ColumnName.AppAddress) { write-host $value.AddressType } would return "Reply" for that line. Commented Jun 20, 2017 at 2:18
  • What happens if a value of a property contains a comma? i.e. [Property=[value1,"value2a,value2b",value3]]. And then, if it contains "? [Property=[value1,"value2a,value2b","value""3"""]]? Commented Jun 20, 2017 at 2:35

3 Answers 3

2

Oh! Sorry...dont see the file content: ,,,,,"[AsymmetricKey=[]]","[AppAddress=[[AddressType=Reply,A‌​ddress=urn:ietf:wg:o‌​auth:2.0:oob]]]","[A‌​ppAuxiliaryId=[]]",,‌​,, Ok...if all file content like this we can do somethisng like:

$patch = get-content 'D:\test\testing!.csv'

$pl = $patch.Length - 1

for ($i=0 ; $i -le $pl ; $i++) {

$patch[$i].Replace(",,,,,","").Replace(",,‌​,,","").Replace("Reply,A‌​ddress","Reply.A‌​ddress").Split(",")[0]
$patch[$i].Replace(",,,,,","").Replace(",,‌​,,","").Replace("Reply,A‌​ddress","Reply.A‌​ddress").Split(",")[1]
$patch[$i].Replace(",,,,,","").Replace(",,‌​,,","").Replace("Reply,A‌​ddress","Reply.A‌​ddress").Split(",")[2]
$patch[$i].Replace(",,,,,","").Replace(",,‌​,,","").Replace("Reply,A‌​ddress","Reply.A‌​ddress").Split(",")[3]
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to search some info, think its can be work (but not sure):

$patch = get-content 'D:\test\testing!.csv' $pl = $patch.Length - 1

for ($i=0 ; $i -le $pl ; $i++) {

$regex = "urn:ietf:wg:o‌​auth:2.0:o2b"

$val = $patch[$i].Replace(",,,,,","").Replace(",,‌​,,","").Replace("Reply,A‌​ddress","Reply.A‌​ddress").Split(",")[1]
  if ($val.Contains($regex)) 
  {
 $val
 }
}

Comments

-1

You try to do something like this:

$csv = Import-Csv Path:\testing!.csv -Header V1

foreach ($line in $csv) {
    $obj = New-Object -TypeName PSObject -Property @{
        First = $line
    } #| select First #
    @{Name='First';Expression={($_.First).Split(";")[1]}}
    $obj1 = $obj | select -ExpandProperty First
    $obj1.V1
}

1 Comment

How does this parse "[Property=[value1,value2,value3]]" into an array?

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.