user1197981
user1197981

Reputation: 163

powershell string/ array modification

I have a csv file with some data. I have read the columns of it using

$read=import-csv C:\file.csv | foreach-object {"{0} {1}" -f $.ColA,$.ColB}

There are 100+ rows for this.

This is how $read looks:

test2pc K:\\
test3pc L:\\
testapc 
testapc C:\\XYZ
testapc C:\\
testapc C:\\
testapc 
testbpc C:\\
testbpc F:\\
testbpc F:\\ABC
testbpc F:\\DEF
testbpc F:\\GHI

I am looking to remove all rows except those which have anything after the "\\", and anything that does not have a "\\". I also am looking to modify those entries, in the end I would be left with

\\testapc\C$\XYZ
\\testbpc\f$\ABC
\\testbpc\F$\DEF
\\testbpc\F$\GHI

Any inputs greatly appreciated.

Upvotes: 0

Views: 314

Answers (1)

Torbjörn Bergstedt
Torbjörn Bergstedt

Reputation: 3429

A Where-Object with a regexp should do the trick:

$read=Import-Csv C:\file.csv | Where-Object { $_.ColB -match '\\\\\w' } | Foreach-Object {"{0} {1}" -f $.ColA,$.ColB}

If you have anything other than letter after the '\\'s you might need to extend the search to include numbers, special characters, etc.

Upvotes: 2

Related Questions