Click here to Skip to main content
15,891,033 members
Articles / All Topics

Splitting CSV File Based on Content in One Line using PowerShell

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Apr 2010CPOL1 min read 25.1K   4   2
Splitting CSV file based on content in one line using PowerShell

Problem

You have a CSV file that contains department employees in a format like this:

Department,Employee
Sales,emp1
HR,emp2
Sales,emp3
Finance,emp4
Finance,emp5
Security,emp6
Security,emp7
Security,emp8
HR,emp9

And you need to split the contents of this file to separate files based on department name. So for the above example, we should get four files, Sales.csv, HR.csv, Finance.csv, and Security.csv. Each file contains only its employees.

And the solution really shows the power of PowerShell pipelining:

Import-Csv file.csv | Group-Object -Property "department" | 
	Foreach-Object {$path=$_.name+".csv" ; $_.group | 
	Export-Csv -Path $path -NoTypeInformation}

Dissecting the above commands:

  • Import-Csv file.csv: Parses the CSV file and returns an array of objects.
  • | Group-Object -Property "department": Since we need to split by department, it makes sense to group objects by the department property.
  • | Foreach-Object {...}: We need to apply an action for each group (department). So we pipeline the resulted groups to Foreach-Object.
  • $path=$_.name+".csv": Within the foreach, we need to create a temporary variable ($path) to be passed to the next pipeline responsible for the actual saving. Note that I use the semicolon ";" to separate this part from the next. And I used the name property of the group (which maps to department name in our case) to format the file name.
  • $_.group | Export-Csv -Path $path -NoTypeInformation: Then for each group we have, we need to export its contents (CSV file rows) to the file path created in the past step. So we again pipeline the group property of the group item (which is an ArrayList of original objects) to the Export-CSV Cmdlt.

And the result should be files like:

Finance.csv:
"Department","Employee"
"Finance","emp4"
"Finance","emp5"

This article was originally posted at http://forloveofsoftware.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
I’m a software architect with a long experience in software architecture and design, with interest in cloud platforms and Agile methodologies.
I worked with many teams to successfully deliver software products using many frameworks and languages, and targeting many platforms like Azure, SharePoint, mobile, and desktop applications.
During my work with Agile teams, I coached them in both the technical and engineering aspects and helped developers with different experience levels in the organization to achieve the best results and build their skills. I also worked with product owners to get the best possible value through a continuous delivery model.
All these activities do not keep me away from coding and keeping up to date with new technologies and exploring new things from cool new libraries to cloud offerings to data science.

Comments and Discussions

 
QuestionFollowup q Pin
five-dts29-Jan-14 10:55
five-dts29-Jan-14 10:55 
AnswerRe: Followup q Pin
Hesham Amin29-Jan-14 18:46
Hesham Amin29-Jan-14 18:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.