Is there any method/code/reference to count the number of columns of every each row of records in a CSV file by maybe using PowerShell Get-Content and without using any Excel API as the records in the CSV file are extremely large (1GB+). Using Excel API such as Import-Csv or New-Object -ComObject Excel.Application will take extremely long periods of time to process.
The following image is the example of records in a CSV file.
And the following image is the same CSV open in Notepad++. As you can see there is only one delimiter in row 4. Hence this row counted as 2 columns while others are 4 columns.
I would like create a script that can check every row of CSV data with short time taken and can detect the columns are missing if the delimiter is not same as the header(as shown in row 4 with 2 columns but in row 1 header with 4 columns). So the final result would be like this:
Row 2 4 Columns Pass
Row 3 4 Columns Pass
Row 4 2 Columns Fail
I'm currently using COMAPI in my completed script, but I want to switch to using Get-Content. However, the quoted comma leads to an incorrect result.
# sample testing code snippet
$path = "testing.csv"
$delimiter = ","
Get-Content $path | ForEach-Object {($_.Split($delimiter)).Count}
And the result from the same CSV file will be like this:
4
4
4
3

