Using PowerShell Filter functions to filter out header rows during CSV file merges

by Klaus Graefensteiner 6/27/2009 8:23:09 AM

The Problem

I needed to merge 365 CSV files that represent daily weather data sets into one CSV file that contains all the data accumulated during one year. Each of the daily CSV files had a header row. The yearly file should only have one. Filtering out rows is a perfect application of the PowerShell filter functions.

Inside a Filter

Figure 1: Inside a Filter

The Solution

This is where PowerShell’s pipeline programming is shining. It only takes a Filter function definition, inserting it into an pre-existing pipeline and you are done.

Here is the sample script:

   1: #Filter Function filters out records that start with "Time"
   2: Filter Filter-Header
   3: {
   4:     if($_ -match "^Time")
   5:     {
   6:         $_ | out-null
   7:     }
   8:     else
   9:     {
  10:         $_
  11:     }
  12: }
  13:  
  14: cd "C:\Users\Klaus\Desktop\TO PLANET\WeatherStation\FilterTest"
  15:   
  16: #Initializing yearly file
  17: $YearlyFileName = "Weather2009.csv"
  18: #Set the header only once in the yearly file
  19: Set-Content -Path $YearlyFileName -value "Time,TemperatureF,DewpointF,PressureIn,WindDirectionDegrees,WindSpeedMPH,WindSpeedGustMPH,Humidity,HourlyPrecipIn" -force -encoding "UTF8"
  20:         
  21: #Open all csv files get-content and add it to the yearly file       
  22: $Files = dir -Path "C:\Users\Klaus\Desktop\TO PLANET\WeatherStation\FilterTest" -Filter "*.csv"
  23:  
  24: #Filter in Action
  25: $Files | ForEach-Object `
  26: {
  27:     Get-Content -Path $_.Name -Encoding "UTF8" | Filter-Header | Add-Content -path $YearlyFileName -Encoding "UTF8"
  28: }

Download

The script file and some sample csv files can be downloaded here: FilterCSVHeaders.zip

Ausblick

Filter are your friends.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Tips & Tricks | PowerShell

Powered by BlogEngine.NET 1.3.0.0
Vanilla Theme by Klaus Graefensteiner

About Klaus Graefensteiner

GRAVATAR icon of Klaus Graefensteiner I enjoy the programming of machines.

E-mail me Send mail
Blogroll as OPML OPML LinkedIn Profile View Klaus Graefensteiner's LinkedIn profile

Calendar

<<  July 2009  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Sign in