Question:
I think that I must be missing something obvious because I’m trying to use Import-CSV to import CSV files that have commented out lines (always beginning with a # as the first character) at the top of the file, so the file looks like this:
1 2 3 4 5 6 7 8 |
#[SpecialCSV],,,,,,,,,,,,,,,,,,,, #Version,1.0.0,,,,,,,,,,,,,,,,,,, #,,,,,,,,,,,,,,,,,,,, #,,,,,,,,,,,,,,,,,,,, #[Table],,,,,,,,,,,,,,,,,,,, Header1,Header2,Header3,Header4,Header5,Header6,Header7,... Data1,Data2,Data3,Data4,Data5,Data6,Data7,... |
I’d like to ignore those first 5 lines, but still use Import-csv to get the rest of the information nicely in to Powershell.
Thanks
Answer:
CSV has no notion of “comments” – it’s just flat data. You’ll need to use Get-Content
and inspect each line. If a line starts with #
, ignore it, otherwise process it.
If you’re OK with using a temp file:
1 2 3 4 5 |
Get-content special.csv |where-object{!$_.StartsWith("#")}|add-content -path $(join-path -path $env:temp -childpath "special-filtered.csv"); $mydata = import-csv -path $(join-path -path $env:temp -childpath "special-filtered.csv"); remove-item -path $(join-path -path $env:temp -childpath "special-filtered.csv") $mydata |format-table -autosize; #Just for illustration |
Edit: Forgot about
convertfrom-csv
. It gets much simpler this way.
1 2 3 4 |
$mydata = Get-Content special.csv | Where-Object { !$_.StartsWith("#") } | ConvertFrom-Csv |