Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created December 20, 2018 16:31
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 gitfvb/b8dfa04fb67770555146c1689468955a to your computer and use it in GitHub Desktop.
Save gitfvb/b8dfa04fb67770555146c1689468955a to your computer and use it in GitHub Desktop.
handling and explore csv files via powershell in an efficient way
# 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