Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Count the number of Columns/rows in a CSV file....

Reply
 
Old 09-16-2007   #1 (permalink)
Faisal


 
 

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

Hi,

I am trying to count the number of columns and rows in a CSV file and place
the values into integer variables so they can be manipulated later. I
thought maybe using the measure-object command to count the number of lines
would be helpful but cant cast this explicitly as an integer (so cant edit
the count values returned). I am not sure how to return the value of the
number of columns, is there a way to count the number of 'noteproperty'
member types using import-csv 'file' | gm ?

Any help into this will be greatly appreciated.

My System SpecsSystem Spec
Old 09-16-2007   #2 (permalink)
Kiron


 
 

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

# assign csv data to a variable (optional)
$csv = import-csv data.csv

# column count
($csv | get-member -type NoteProperty).count

# ...or

(import-csv data.csv | get-member -type NoteProperty).count

# row count
# enclose the object in
# array notation '@()'
# ensuring the count of scalar objects

@($csv).count

# ...or

@(import-csv data.csv).count


--
Kiron

My System SpecsSystem Spec
Old 09-16-2007   #3 (permalink)
Faisal


 
 

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

Excellent, thanks for the help!

"Kiron" wrote:
Quote:

> # assign csv data to a variable (optional)
> $csv = import-csv data.csv
>
> # column count
> ($csv | get-member -type NoteProperty).count
>
> # ...or
>
> (import-csv data.csv | get-member -type NoteProperty).count
>
> # row count
> # enclose the object in
> # array notation '@()'
> # ensuring the count of scalar objects
>
> @($csv).count
>
> # ...or
>
> @(import-csv data.csv).count
>
>
> --
> Kiron
>
My System SpecsSystem Spec
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
Reply

Thread Tools


Similar Threads
Thread Forum
Count number of times a box is ticked in Access 2003 Microsoft Office
How do I filter rows in one csv file matching a value in another PowerShell
add an empty line to txt file every two rows. PowerShell
Re: how to count number of certain char within string PowerShell
count number of files PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46