Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Processing "Sets" of ojects from a CSV file

$
0
0

Hello All

Can someone kindly help me with the following query please.

This is a little bit difficult to explain but I will do my best,

I have a CSV file, the format of the CSV file stays the same but the contents can change (e.g. extra lines may be added to the CSV file) I have simplified the contents of the CSV below but the principle is the same.

Filed1,Field2,Field3
Fred,X,C:\Temp 
Bert,W,C:\Temp2
Ernie,X,C:\Temp
John,R,C:\Temp3
Mike,X,C:\Temp2

Now the important field here is Filed3 this is the field I need to find duplicates on, because where there are duplicates I need to process them as a "set" latter on in my script.

So in the above example there are two occurances of C:\Temp and also two occurances of C:\Temp2 in field 3

First I get myself a PowerShell object by reading in the CSV and sorting on field 3

$Obj = import-csv c:\MyCsv.csv | sort Field3

So not I have

Field1  Filed2   Field3
------    ------    ------
Fred     X         C:\Temp
Ernie    X        C:\Temp
Bert     W       C:\Temp2
Mike     X        C:\Temp2
John     R        C:\Temp3


Latter in the script I need to iterate through these one at a time (e.g foreach ($item in $obj) )

However where there are duplicates on rows 1,2 (C:\Temp and C:\Temp) and rows 3,4 (C:\Temp2 and C:\Temp2) I need to treat each these as a set. Where there is no duplication e.g. the last row in this example I also need to treat these as

a set too (a set with one item in it)

again the CSV file can change at any time, so I do not know how many sets I will and up with when every I read in the CSV file

So I am thinking perhaps the solution is along the lines of, I need to

1: Know how many "sets" I have in total 3 in the above example, (first set containing two items the second set contaning two items and the last set containing one item).

2: I then create X number of new objects X being the number of sets then each new object can hold one set each

so $Obj1 would contain

Field1   Filed2   Field3
------    ------     ------
Fred     X         C:\Temp
Ernie    X         C:\Temp

$Obj2 would contain

Field1   Filed2   Field3
------    ------      ------
Bert      W        C:\Temp2
Mike      X         C:\Temp2

and $Obj3 would contain

Field1   Filed2     Field3
------      ------      ------
John      R           C:\Temp3


I then need to process each of these objects separately in turn through the same foreach object loop.

The objects sets can be deleted as soon as they are used (sent through the foreach loop) therefore perhaps rather than creating three objects I can create one temporary object $objX containing the current set to work on, send this

through the foreach loop then $objX can be overwritten with the next set to be processed by the foreach loop etc until all sets have been processed.

I would be very grateful for help on this please

Ernie

 

 

 

 


Viewing all articles
Browse latest Browse all 10624

Trending Articles