PowerShell: deleting file records based on a condition

Given a file like this, where the second column is [Gender] and the third column is [Human], give me only rows which are [F](offset 20 from 0 – note it is zero and not 1) and [Human] (offset 30). Write the result to a file [HumanFemales.txt].

Dennis M Y
Jan F Y
Emma F Y
Bonzo M N

Names01

This reads the source file content into a variable, counts the records, filters the records based on the condition into an array, and writes the array to a file:

$data = Get-Content -Path C:\temp\names.txt
$data.Count
$filteredData = @()
$data | % {if ($_[19] -eq "F" -and $_[29] -eq "Y") { $filteredData += $_} }
$filteredData.Count
$filteredData | Out-File -FilePath c:\temp\HumanFemales.txt
gc C:\temp\HumanFemales.txt

WriteFile01

WriteFile02

For a file of about 50,000 records on a good I7 box, the records take about 3 minutes to write to the array, uses (depending on the record length) about 1GB of memory, and takes about 5 seconds to write to disk.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s