2

I am having below data in my csv file

"Server Name","Control Group","Tape Number","Status"
"BR502","QCLDUSYSW","Q23028","ACTIVE"
"BR502","QCLDUSYSW","Q32521","ACTIVE"
"BR502","QCLDUSYSW","Q05599","ACTIVE"
"BR503","QCLDUIPLW","Q25582","ACTIVE"
"BR503","QCLDUIPLW","Q15529","ACTIVE"
"BR503","QCLDUIPLW","Q05109","ACTIVE"
"BR504","QCLDUSYSW","Q14445","ACTIVE"
"BR504","QCLDUSYSW","Q27785","ACTIVE"
"BR504","QCLDUUSRD","Q26071","ACTIVE"
"BR505","QCLDUGRPD","Q27657","ACTIVE"
"BR505","QCLDUIPLW","Q17404","ACTIVE"

Please let me know how to break it based on server name like below

"Server Name","Control Group","Tape Number","Status"
"BR502","QCLDUSYSW","Q23028","ACTIVE"
"BR502","QCLDUSYSW","Q32521","ACTIVE"
"BR502","QCLDUSYSW","Q05599","ACTIVE"

"Server Name","Control Group","Tape Number","Status"
"BR503","QCLDUIPLW","Q25582","ACTIVE"
"BR503","QCLDUIPLW","Q15529","ACTIVE"
"BR503","QCLDUIPLW","Q05109","ACTIVE"

so on for rest of the servers.

2
  • Do you want each group on different files, or you just want it separated but on the same file? Commented Oct 6, 2021 at 17:53
  • Not in files. different arrays may be. coz I have to do calculation on each set Commented Oct 6, 2021 at 17:54

2 Answers 2

4

Use the Group-Object cmdlet to group the records together based on a particular property:

Import-Csv .\input.csv |Group-Object -Property 'Server Name' |ForEach-Object {
  # for each group, output a new CSV file with just the records pertaining to that server, named after the server
  $_.Group |Export-Csv .\$($_.Name).csv -NoTypeInformation
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might do what you want:

import-csv tst.csv | where 'Server Name' -eq 'BR502'

If you want to examine each set you would use a Group-Object to bunch it on Server Name like this:

Import-Csv tst.csv | Group-Object 'Server Name'

This gives an object that has two properties Name, Count and Group which contain the Value you groped by, the number of items in the group and the list of items in the group. This can be used for calculations on each group. For example if you want to know how many in Control Groups in each server end with D you could use this:

Import-Csv tst.csv | Group-Object 'Server Name' | Foreach-Object { $name=$_.Name; Write-Output $_.Group | Where-Object 'Control Group' -LIKE '*D' | Measure-Object | Select-Object @{Label='Name';Expression={$name}},Count }

If you use aliases that command can be shortened to:

ipcsv tst.csv | Group 'Server Name' | % { $name=$_.Name; $_.Group | ? 'Control Group' -LIKE '*D' | Measure | Select @{L='Name';E={$name}},Count }

1 Comment

Thanks for the answer, but is it possible not to mention the name of the server? coz this is good for one set. what about others?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.