View Single Post
Old 09-26-2007   #4 (permalink)
Flowering Weeds


 
 

Re: Count the number of Columns/rows in a CSV file....

Quote:

> I am trying to count the number
> of columns and rows in a CSV file
Perhaps Microsoft's Log Parser 2.2

@"
sphere,a.b.c,circle
cube,1.2.3,square
pyramid,apples.orange.pears,triangle
"@ | out-file noFieldNames.csv -encoding ascii

LogParser.exe -h: -i:csv -headerRowff noFieldNames.csv | `
select-object -last 11 | select-object -first 4

Returns:

Fields:

Filename (S) RowNumber (I)
Field1 (S) Field2 (S) Field3 (S)

@"
type,many parts,object
sphere,a.b.c,circle
cube,1.2.3,square
pyramid,apples.orange.pears,triangle
"@ | out-file withFieldNames.csv -encoding ascii

LogParser.exe -h: -i:csv -headerRown withFieldNames.csv | `
select-object -last 11 | select-object -first 4

Returns (with field names):

Fields:

Filename (S) RowNumber (I) type (S)
many parts (S) object (S)

And if you script it:

$a = LogParser.exe -h: -i:csv -headerRown withFieldNames.csv | `
select-object -last 11 | select-object -first 4

$b = [system.string]::Join(" ",$a)

($b.Split(")").Count -1) -2

returns

3

And now for the rows:

LogParser.exe "SELECT COUNT(*) FROM noFieldNames.csv" `
-i:csv -headerRowff -statsff -qn

LogParser.exe "SELECT COUNT(*) FROM withFieldNames.csv" `
-i:csv -headerRown -statsff -qn

Both return 3 and

LogParser.exe "SELECT COUNT(*) FROM withFieldNames.csv" `
-i:csv -headerRowff -statsff -qn

returns 4

Oh and one can do this with other file types too!

Just another way!




My System SpecsSystem Spec