0

I have a csv file which conatins line something similar to this

"Some KIND of Test","[STATUS]","TESTNAME","VMNAME","TESTTME","SOME PARAMETER"

I am trying to parse this. I am using split to get STATUS and TESTNAME

$col = $line.split('","') #used .split('`",`"') as well
$col.length #output value is 18
#cl = $line.split(',')
$cl.length #output value is 6

Why am I seeing two different values here. In both cases ' and "," present same number of times

It seems like I am making some basic mistake. I couldn't figure it out. Any help would be grateful. Thanks

srikanth peetha
  • 229
  • 1
  • 3
  • 17

2 Answers2

2

The only resolvable overload for String.Split() treats the "," string argument as a character array and splits on each occurrence of any of the characters in the array.

Use the -split regex operator instead:

$col = $line -split '","'

If you really hate regex, you can force an overload of String.Split() that takes strings as separators by explicitly casting the first input argument to a string array:

$col = $line.Split([string[]]@('","'),[System.StringSplitOptions]::None)
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
2

Why you dont use import-csv command like it?

import-csv "yourpathtofile"

if you want really use get-content you cando it too:

$string='"Some KIND of Test","[STATUS]","TESTNAME","VMNAME","TESTTME","SOME PARAMETER"'
$string | ConvertFrom-Csv -Delimiter "," -Header p1, status, testname, p4, p5 | select status, testname
Esperento57
  • 16,521
  • 3
  • 39
  • 45