Merge Columns from CSV - powershell

I've got a CSV like this:
Group;Name;Color
Fruit;Apple;green
Vegetable;Carrot;orange
Fruit;Banana;yellow
Fruit;cherry;red
Vegetable;cucumber;green
and want to merge it (via PowerShell) so that each Group appears only one time and the according 'Names' next to it in an Array(?), like this:
Group;Name;color
Fruit;{Apple,Banana,Cherry};{green,yellow,red}
Vegetable;{Carrot;cucumber};{orange,green}

Use Group-Object for grouping objects by their properties:
Import-Csv 'C:\path\to\input.csv' -Delimiter ';' |
Group-Object Group |
select #{n='Group';e={$_.Name}},
#{n='Name';e={'{{{0}}}' -f ($_.Group.Name -join ',')}},
#{n='Color';e={'{{{0}}}' -f ($_.Group.Color -join ',')}} |
Export-Csv 'C:\path\to\output.csv' -NoType -Delimiter ';'

Related

Select-Object not selecting object from csv-file

I'm trying to import some csv files to further work with them and export them in the end. They all have two header lines from which i'll only need the second one. I also need to delete most columns except a few. Unfortunately it seems you'll need to decide if you want to skip rows with get-content or exclude columns with import-csv. Neither of those can't do both, so i got a workaround:
$out="bla\bla\out.csv"
$in="bla\bla\in.csv"
$header= (get-content $in -TotalCount 2 )[-1]
$out = Import-csv $in -Header $header -Delimiter ";"|select column1 | Export-Csv -Path $out -NoTypeInformation
this returns an empty csv with the header name column1. What am i doing wrong?
Edit:
The input csv looks like:
filename;filename;...
column1;column2;...
1;a;...
2;b;...
...
I guess that -Header can't read arrays without single quotation marks, so i'm trying to find a solution to that atm.
If you know the name of the header you want to filter on, the following should do the trick and only requires reading the file once:
$out = "out.csv"
$in = "in.csv"
Get-Content $in | Select-Object -Skip 1 |
ConvertFrom-Csv -Delimiter ';' | Select-Object column1 |
Export-Csv $out -NoTypeInformation
If however, you don't know the name of the header you need to filter on (column1 on example above) but you know it's the first column, it would require an extra step:
$csv = Get-Content $in | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter ';'
$csv | Select-Object $csv[0].PSObject.Properties.Name[0] | Export-Csv $out -NoTypeInformation
We can get the first object of object array ($csv[0]) and get it's properties by accessing it's PSObject.Properties then select the 1st property (.Name[0] - column1 in this case).

Get Unique Column and Count from CSV file in Powershell

I have a CSV File called Products.csv
Product_ID,Category
1,A
2,A
3,A
4,B
I want a powershell script that will show me the Unique Categories along with the Count and export to CSV.
i.e.
A,3
B,1
I have used the following code to extract the Unique Categories, but cannot get the Count:
Import-Csv Products.csv -DeLimiter ","|
Select 'Category' -Unique |
Export-Csv Summary.csv -DeLimiter "," -NoTypeInformation
Can anyone help me out?
Thanks.
You can use Group-Object to get the count.
Import-Csv Products.csv -DeLimiter "," |
Group-Object Category | Foreach-Object {
"{0},{1}" -f $_.Name,$_.Count
}
If you want a CSV output of the count, you need headers for your data. Group-Object outputs property Name which is the grouped property value and Count which is the number of items in that group.
Import-Csv Products.csv -DeLimiter "," |
Group-Object Category | Select-Object Name,Count |
Export-Csv Summary.csv -Delimiter ',' -NoType
You can take the above code a step further and use Select-Object's calculated properties. Then you can create custom named columns and/or values with expressions.
Import-Csv Products.csv -DeLimiter "," |
Group-Object Category |
Select-Object #{n='Product_ID';e={$_.Name}},Count |
Export-Csv Summary.csv -Delimiter ',' -NoType

Spliting a csv file based on the value of one column

I have a csv file that I have to split based on the value of a column.
I'm using the following script to do so:
Import-Csv test.csv | Group-Object -Property "Nr dep" |
Foreach-Object {$path=$_.name+".csv" ; $_.group |
Export-Csv -Path E:\PowerShell\script\$path -NoTypeInformation}
The file is split to files based on the Nr dep value column but with quotes and it works only with comma delimited csv files.
I tried to use -replace but still no result (maybe I'am writing it in a bad manner)
Import-Csv test.csv | Group-Object -Property "Nr dep" |
Foreach-Object {$path=$_.name+".csv" ; ($_.group |
ConvertTo-Csv -NoTypeInformation) -replace '"', "" | Out-File E:\PowerShell\script\$path -Force}
1) How can I make the delimitation a semi comma instead of a comma
2) how can I get rid of the quotes
3) is it possible to have an .xlsx outfile instead of a .csv file
1) You can specify the delimiter by using the -Delimiter parameter:
Import-Csv test.csv -Delimiter ';'
2) By using -replace '"' which you already do.
3) You will need a framework // application for that.

How to select required columns in csv file using powershell

I have a csv file with data looking like this.
Div,Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR
I1,20/08/16,Juventus,Fiorentina,2,1,H,1,0,H
I1,20/08/16,Roma,Udinese,4,0,H,0,0,D
I1,21/08/16,Atalanta,Lazio,3,4,A,0,3,A
I1,21/08/16,Bologna,Crotone,1,0,H,0,0,D
I am trying to export first six columns like below to a new csv file.
Div,Date,HomeTeam,AwayTeam,FTHG,FTAG
I1,20/08/16,Juventus,Fiorentina,2,1
I1,20/08/16,Roma,Udinese,4,0
I1,21/08/16,Atalanta,Lazio,3,4
I1,21/08/16,Bologna,Crotone,1,0
I am using the following powershell command.
Import-Csv $infile -DeLimiter ","|
Select'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG'|
Export-Csv $trimfile -DeLimiter "," -NoTypeInformation
But my output csv file looks like this
"Div","Date","HomeTeam","AwayTeam","FTHG","FTAG"
"I1","20/08/16","Juventus","Fiorentina","2","1"
"I1","20/08/16","Roma","Udinese","4","0"
"I1","21/08/16","Atalanta","Lazio","3","4"
"I1","21/08/16","Bologna","Crotone","1","0"
The no.of columns are much higher in the actual file. Simplified it here. What am I doing wrong?
Try with this code :
Import-Csv $infile -DeLimiter "," |
Select-Object 'Div','Date','HomeTeam','AwayTeam','FTHG','FTAG' |
ConvertTo-Csv -NoTypeInformation |
ForEach-Object { $_ -replace '"', ""} |
Out-File $trimfile

Delete line in CSV if two columns not equal

I have big CSV Files, here some example of content:
Name;Number;Type;AlterName
Prag;1418;2;2012;Prag
Prag;1836;3;2012;Prag
Prag;1836;514;2012;Moscow
...
And I need delete the line where is not equal Name and AlterName.
In this case:
Prag;1836;514;2012;Moscow
Simply check if the fields are equal.
Import-Csv 'C:\path\to\input.csv' -Delimiter ';' |
Where-Object { $_.Name -eq $_.AlterName } |
Export-Csv 'C:\path\to\output.csv' -Delimiter ';' -NoType