Just recently at work we had to import a large dataset into a database, but two of the columns were “category” and “subcategory”. Naturally we wanted these in a separate table, so we needed a way of parsing through the existing data and get the unique categories, and the unique matching subcategories for each.
Excel:
Firstly, there’s the Excel ‘07 way (most likely also possible in other versions too):
Select the two columns that hold your categories and subcategories. Go to the “Advanced” menu item in Filtering:
The presented dialog has the option of “Unique Records Only”
Done, it’s the equivalent of doing a “distinct” in SQL. Now sort the data, and you’re ready to go.
Powershell:
But no discussion of parsing/manipulating data is complete without mentioning Powershell – surely we can do this in PS. Let’s assume that the data was saved in csv form (you could always save your Excel spreadsheet as .csv).
Luckily, there’s already a command that is able to parse csv’s for us: “Import-Csv”. This cmdlet will parse the csv and create objects that have properties named after the columns – in the below image you can see that the object has a “Category” and “SubCategory” property.
Now that we have this collection of objects, use your favourite way of flushing out duplicates. I’m keen on building up a new list, checking on each insert that the same entry doesn’t exist:
- $temp = @{}
- $data | foreach {
- if ($temp.ContainsKey($_.Category))
- {
- #check if subcategory exists, insert subcategory if doesnt
- if ($temp[$_.Category].ContainsValue($_.Subcategory) -eq $false)
- {
- $temp[$_.Category].Add($_.Subcategory, $_.Subcategory);
- }
- }
- else
- {
- #put it in:
- $temp.Add($_.Category, @{$_.Subcategory = $_.Subcategory});
- }
- }
This is all well and good, but there are issues. The "Category” property names are hardcoded, and it depends on an existing $data variable. Ideally we want a cmdlet that can support piping, and will let us specify the names of the columns.
Luckily for us, Powershell is able support the following constructs:
What happened there is by putting the variable in brackets, PS will evaluate the variable and put it in the script. I was able to create a variable $somestring, which had a string value of “Subcategory”. When I used it in brackets, it expanded out to “Subcategory” in the script, and functioned in the same way as $data[2].Subcategory. Neat. This is an invaluable feature in scripting.
So we replacing all places where we previously had “Category” and “Subcategory” with expansions, and add them as required parameters.
Last thing, we need to get rid of the assumption of pre-existing $data variable. Ideally the input will be piped in. No problem, the reserved $input variable is the piped in parameters.
So now we can call it like so:
import-csv c:\MyTest.csv | c:\categoryParser.ps1 "Category" "Subcategory"
The final script looks like this:
- param (
- $categoryName = $(throw "Specify category column name" ),
- $subcategoryName = $(throw "specify subcategory name")
- )
- $temp = @{}
- $input | foreach {
- if ($temp.ContainsKey($_.($categoryName)))
- {
- #check if subcategory exists, insert subcategory if doesnt
- if ($temp[$_.($categoryName)].ContainsValue($_.($subcategoryName)) -eq $false)
- {
- $temp[$_.($categoryName)].Add($_.($subcategoryName), $_.($subcategoryName));
- }
- }
- else
- {
- #put it in:
- $temp.Add($_.($categoryName), @{$_.($subcategoryName) = $_.($subcategoryName)});
- }
- }
- #dump out the output
- $temp
And produces something like this, a hashtable of objects each of which has the category name, and a hashtable of subcategories.
From here it would be easy to traverse them all with two nested foreach loops, and do whatever with them, like generating INSERT statements.