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

0

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That is pretty straight forward.
If you're on version 2.0 where -split is not available Why did you say that?
@PetSerAl Because my memory is bad, and it's poorly documented
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.