Skip to content

Instantly share code, notes, and snippets.

@vermorel
Created March 27, 2014 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vermorel/9804025 to your computer and use it in GitHub Desktop.
Save vermorel/9804025 to your computer and use it in GitHub Desktop.
Create a sample large flat files (keeping only 1 line out of N)
# Sample-Content
# By Joannes Vermorel, 2014
# Keep only 1 line out of N of a flat file
# Usage: sample-content .\myfile.csv 100
function Sample-Content()
{
param
(
[string]$path = $null,
[int]$ratio = 100
);
$path = (get-item $path).FullName;
$outpath = $path + ".sample";
$count = -1; # preserve the first line to keep the headers (if any)
$r = [IO.File]::OpenText($path)
while ($r.Peek() -ge 0)
{
$line = $r.ReadLine();
$count = $count + 1;
if(($count % $ratio) -eq 0)
{
$line | out-file $outpath -Append;
}
}
$r.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment