tags:

views:

43

answers:

1

ok, I give up! why doesn't this work? Im just trying to loop through a csv file and replace any value in the nth column with some value.

$source = "C:\blah.csv"

(gc $source) | foreach{ $_.Split(',')[10] = 'something'} | sc $source
A: 

Basically, what you are trying is something like that:

$s = 'a,b,c,d,e'
$s.Split(',')[4] = 'something'
$s

That won't work, because you don't assign 'something' to any variable.

I would either read the file as csv (via Import-CSV) or (if it structure is really simple) use regexes:

$s = 'c,d,e', 'c,d,x', 'z,f,d'
$s | % { $_ -replace '(?<=([^,]*,){2}).*','something'}
stej
I wonder if he is trying to compare against 'something' and should be using `-eq`?
Keith Hill
what do you mean "read the file to csv"?
Peter Goras
@Keith, for me it makes more sense to replace some value. But you might be right. Peter didn't tell us what he wanted to do.
stej
@Peter, look at e.g. http://technet.microsoft.com/en-us/library/ee176874.aspx Just read it as csv and ... (it's up to you) ;)
stej