Created
December 20, 2018 16:31
-
-
Save gitfvb/b8dfa04fb67770555146c1689468955a to your computer and use it in GitHub Desktop.
handling and explore csv files via powershell in an efficient way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check some rows in between | |
cd "<path>" | |
$file = Get-Item -path "filename.csv" | |
$readFirstLine = $true | |
$startLine = 121200 | |
$endLine = 121400 | |
$reader = New-Object System.IO.StreamReader($file.FullName, [System.Text.Encoding]::UTF8) | |
$i = 0 | |
$rows = @() | |
# read first line, maybe the header? | |
if ( $readFirstLine ) { | |
$rows += $reader.ReadLine() | |
} | |
$i++ | |
# jump to the important line | |
while ($reader.Peek() -ge 0 -and $i -lt $startLine) { | |
$reader.ReadLine() > $null | |
$i++ | |
} | |
# save the following lines | |
while ($reader.Peek() -ge 0 -and $i -lt $endLine) { | |
$rows += $reader.ReadLine() | |
$i++ | |
} | |
$reader.Close() | |
# write that part into a file | |
$rows | Set-Content -Path "test.csv" -Encoding UTF8 | |
# check those rows for the numbers of delimiters | |
$rows | ForEach { | |
$_.Split(",").Count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment