你可以将带括号和数组格式的字符串转换为数组吗?

时间:2017-06-20 01:29:51

标签: arrays powershell csv

我正在处理CSV文件,其中一列是具有数组格式的字符串的单元格。以下是访问这些单元格的内容:

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

输出:

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

您可以看到每个单元格都输出一个带有数组结构的字符串。我想将每个单独的字符串视为具有Property[0] = value1等的数组

有一种简单的方法吗?否则,我认为我需要使用Reg Ex。

3 个答案:

答案 0 :(得分:2)

哦!抱歉...没有看到文件内容:,,,,,“[AsymmetricKey = []]”,“[AppAddress = [[AddressType = Reply,A ddress = urn:ietf:wg:o auth:2.0: OOB]]]”, “[A ppAuxiliaryId = []]” ,, ,, 好的...如果像这样的所有文件内容,我们可以做类似的事情:

$ patch = get-content'D:\ test \ testing!.cs​​v'

$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]
}

答案 1 :(得分:0)

  

如果您想搜索某些信息,请认为它可以正常工作(但不确定):

     

$ patch = get-content'D:\ test \ testing!.cs​​v'       $ 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
 }
}

答案 2 :(得分:-1)

你尝试做这样的事情:

$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
}