Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

using export-csv to export to multiple CSV files

Closed Thread
 
Thread Tools Display Modes
Old 05-22-2008   #1 (permalink)
Srinivas
Guest


 

using export-csv to export to multiple CSV files

Dear all,
I am trying to read from a CSV file using import-csv cmdlet and trying to
extract relevant information using export-csv it into 3CSV files(exporting to
3 files is mandatory for another process to read them).
I could not figure out to write in a single line to import the source CSV
file and extract into 3 or more files so I am doing it in three lines.

Below are more details
##########################
#This is source CSV file
Time,CPU%,Memory,Network,disk
10:00:01,2,2048,100,3
10:01:01,3,4096,100,4
..
..
..
..
#end of file
############################

ps1 script below that I use
#################################################
#Script to read the source and write to three different CSV files
$a = import-csv Source.csv
$a | select 'Time','CPU*' | Export-Csv cpu.csv -NoType -noClobber
$a | select 'Time','Memory'| Export-Csv Memory.csv -NoType -noClobber
$a | select 'Time','Network | Export-Csv Network.csv -NoType -noClobber
#end of script
#################################################

Is there a better way to do this job then what I am currently doing?
Currently it is doing the job but because of number of columns in the source
file are more than 1000 and the source CSV files are like 25MB it takes 1/2
for extracting each source CSV file

Appreciate any kind of feedback
Old 05-23-2008   #2 (permalink)
RichS [MVP]
Guest


 

RE: using export-csv to export to multiple CSV files

You could try something like this


Import-Csv source.csv | foreach {
Out-File cpu.csv -InputObject "$($_.Time),$($_.CPU)" -Append
Out-File memory.csv -InputObject "$($_.Time),$($_.Memory)" -Append
Out-File network.csv -InputObject "$($_.Time),$($_.Network)" -Append
}

Export-csv doesn't have an append option which is why I've had to change to
out-file. Notice that I've had to change CPU% to CPU to get this to work




--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Srinivas" wrote:
Quote:

> Dear all,
> I am trying to read from a CSV file using import-csv cmdlet and trying to
> extract relevant information using export-csv it into 3CSV files(exporting to
> 3 files is mandatory for another process to read them).
> I could not figure out to write in a single line to import the source CSV
> file and extract into 3 or more files so I am doing it in three lines.
>
> Below are more details
> ##########################
> #This is source CSV file
> Time,CPU%,Memory,Network,disk
> 10:00:01,2,2048,100,3
> 10:01:01,3,4096,100,4
> .
> .
> .
> .
> #end of file
> ############################
>
> ps1 script below that I use
> #################################################
> #Script to read the source and write to three different CSV files
> $a = import-csv Source.csv
> $a | select 'Time','CPU*' | Export-Csv cpu.csv -NoType -noClobber
> $a | select 'Time','Memory'| Export-Csv Memory.csv -NoType -noClobber
> $a | select 'Time','Network | Export-Csv Network.csv -NoType -noClobber
> #end of script
> #################################################
>
> Is there a better way to do this job then what I am currently doing?
> Currently it is doing the job but because of number of columns in the source
> file are more than 1000 and the source CSV files are like 25MB it takes 1/2
> for extracting each source CSV file
>
> Appreciate any kind of feedback
Old 05-23-2008   #3 (permalink)
Srinivas
Guest


 

RE: using export-csv to export to multiple CSV files

Thanks Rich for your response.
My concern regarding changing the CPU% to CPU.

Some of the values that I am ACTUALLY extracting from the source CSV file
are as below, I wanted to keep it simple while explaining in my initial
request:

(PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Physical Cpu(*)\% Processor Time,
(PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Logical Cpu(*)\% Processor Time,
(PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Memory\Console MBytes,
\\F*ESX*\Memory\Kernel MBytes, \\F*ESX*\Memory\NonKernel MBytes,
\\F*ESX*\Memory\Free MBytes, \\F*ESX*\Memory\Kernel Reserved MBytes,
\\F*ESX*\Console Memory\Free MBytes, \\F*ESX*\Memory\Kernel Unreserved MBytes

So I am wondering if there is a way to modify the script you mentioned with
all the back slashes and curly brackets and percentage symbols in the CSV
file.

Regards


"RichS [MVP]" wrote:
Quote:

> You could try something like this
>
>
> Import-Csv source.csv | foreach {
> Out-File cpu.csv -InputObject "$($_.Time),$($_.CPU)" -Append
> Out-File memory.csv -InputObject "$($_.Time),$($_.Memory)" -Append
> Out-File network.csv -InputObject "$($_.Time),$($_.Network)" -Append
> }
>
> Export-csv doesn't have an append option which is why I've had to change to
> out-file. Notice that I've had to change CPU% to CPU to get this to work
>
>
>
>
> --
> Richard Siddaway
> All scripts are supplied "as is" and with no warranty
> PowerShell MVP
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Srinivas" wrote:
>
Quote:

> > Dear all,
> > I am trying to read from a CSV file using import-csv cmdlet and trying to
> > extract relevant information using export-csv it into 3CSV files(exporting to
> > 3 files is mandatory for another process to read them).
> > I could not figure out to write in a single line to import the source CSV
> > file and extract into 3 or more files so I am doing it in three lines.
> >
> > Below are more details
> > ##########################
> > #This is source CSV file
> > Time,CPU%,Memory,Network,disk
> > 10:00:01,2,2048,100,3
> > 10:01:01,3,4096,100,4
> > .
> > .
> > .
> > .
> > #end of file
> > ############################
> >
> > ps1 script below that I use
> > #################################################
> > #Script to read the source and write to three different CSV files
> > $a = import-csv Source.csv
> > $a | select 'Time','CPU*' | Export-Csv cpu.csv -NoType -noClobber
> > $a | select 'Time','Memory'| Export-Csv Memory.csv -NoType -noClobber
> > $a | select 'Time','Network | Export-Csv Network.csv -NoType -noClobber
> > #end of script
> > #################################################
> >
> > Is there a better way to do this job then what I am currently doing?
> > Currently it is doing the job but because of number of columns in the source
> > file are more than 1000 and the source CSV files are like 25MB it takes 1/2
> > for extracting each source CSV file
> >
> > Appreciate any kind of feedback
Old 05-23-2008   #4 (permalink)
RichS [MVP]
Guest


 

RE: using export-csv to export to multiple CSV files

Hmmmmmm

That may be a bit more problematic. Could you post the first 6 or 8 lines
of your source.csv file so that I can see what is actually coming through.
Also if you could post which fields you actually want to go in which files

Thanks

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Srinivas" wrote:
Quote:

> Thanks Rich for your response.
> My concern regarding changing the CPU% to CPU.
>
> Some of the values that I am ACTUALLY extracting from the source CSV file
> are as below, I wanted to keep it simple while explaining in my initial
> request:
>
> (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Physical Cpu(*)\% Processor Time,
> (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Logical Cpu(*)\% Processor Time,
> (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Memory\Console MBytes,
> \\F*ESX*\Memory\Kernel MBytes, \\F*ESX*\Memory\NonKernel MBytes,
> \\F*ESX*\Memory\Free MBytes, \\F*ESX*\Memory\Kernel Reserved MBytes,
> \\F*ESX*\Console Memory\Free MBytes, \\F*ESX*\Memory\Kernel Unreserved MBytes
>
> So I am wondering if there is a way to modify the script you mentioned with
> all the back slashes and curly brackets and percentage symbols in the CSV
> file.
>
> Regards
>
>
> "RichS [MVP]" wrote:
>
Quote:

> > You could try something like this
> >
> >
> > Import-Csv source.csv | foreach {
> > Out-File cpu.csv -InputObject "$($_.Time),$($_.CPU)" -Append
> > Out-File memory.csv -InputObject "$($_.Time),$($_.Memory)" -Append
> > Out-File network.csv -InputObject "$($_.Time),$($_.Network)" -Append
> > }
> >
> > Export-csv doesn't have an append option which is why I've had to change to
> > out-file. Notice that I've had to change CPU% to CPU to get this to work
> >
> >
> >
> >
> > --
> > Richard Siddaway
> > All scripts are supplied "as is" and with no warranty
> > PowerShell MVP
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "Srinivas" wrote:
> >
Quote:

> > > Dear all,
> > > I am trying to read from a CSV file using import-csv cmdlet and trying to
> > > extract relevant information using export-csv it into 3CSV files(exporting to
> > > 3 files is mandatory for another process to read them).
> > > I could not figure out to write in a single line to import the source CSV
> > > file and extract into 3 or more files so I am doing it in three lines.
> > >
> > > Below are more details
> > > ##########################
> > > #This is source CSV file
> > > Time,CPU%,Memory,Network,disk
> > > 10:00:01,2,2048,100,3
> > > 10:01:01,3,4096,100,4
> > > .
> > > .
> > > .
> > > .
> > > #end of file
> > > ############################
> > >
> > > ps1 script below that I use
> > > #################################################
> > > #Script to read the source and write to three different CSV files
> > > $a = import-csv Source.csv
> > > $a | select 'Time','CPU*' | Export-Csv cpu.csv -NoType -noClobber
> > > $a | select 'Time','Memory'| Export-Csv Memory.csv -NoType -noClobber
> > > $a | select 'Time','Network | Export-Csv Network.csv -NoType -noClobber
> > > #end of script
> > > #################################################
> > >
> > > Is there a better way to do this job then what I am currently doing?
> > > Currently it is doing the job but because of number of columns in the source
> > > file are more than 1000 and the source CSV files are like 25MB it takes 1/2
> > > for extracting each source CSV file
> > >
> > > Appreciate any kind of feedback
Old 05-23-2008   #5 (permalink)
Srinivas
Guest


 

RE: using export-csv to export to multiple CSV files

The post is not allowing more than 30000 characters, so I am unable to post
even two lines from my source.csv. Below is precisely what I am trying to
extract though.

$a = import-csv Source.csv
$a | select '(PDH-CSV 4.0) (EDT)(0)','\\F*ESX*\Physical Cpu(*)\% Processor
Time' | Export-Csv ESX01-Pcpu.csv -NoType -noClobber
$a | select '(PDH-CSV 4.0) (EDT)(0)','\\F*ESX*\Logical Cpu(*)\% Processor
Time' | Export-Csv ESX01-Lcpu.csv -NoType -noClobber
$a | select '(PDH-CSV 4.0) (EDT)(0)','\\F*ESX*\Memory\Console
MBytes','\\F*ESX*\Memory\Kernel MBytes','\\F*ESX*\Memory\NonKernel
MBytes','\\F*ESX*\Memory\Free MBytes','\\F*ESX*\Memory\Kernel Reserved
MBytes','\\F*ESX*\Console Memory\Free MBytes','\\F*ESX*\Memory\Kernel
Unreserved MBytes' | Export-Csv ESX01-Memory.csv -NoType -noClobber
$a | select '(PDH-CSV 4.0) (EDT)(0)','\\F*ESX*\Physical
Disk(vmhba32)\Commands/sec','\\F*ESX*\Physical
Disk(vmhba32)\Reads/sec','\\F*ESX*\Physical
Disk(vmhba32)\Writes/sec','\\F*ESX*\Physical Disk(vmhba32)\MBytes
Read/sec','\\F*ESX*\Physical Disk(vmhba32)\MBytes
Written/sec','\\F*ESX*\Physical Disk(vmhba32)\Average Driver
MilliSec/Command','\\F*ESX*\Physical Disk(vmhba32)\Average Kernel
MilliSec/Command','\\F*ESX*\Physical Disk(vmhba32)\Average Guest
MilliSec/Command','\\F*ESX*\Physical Disk(vmhba32)\Average Queue
MilliSec/Command','\\F*ESX*\Physical Disk(vmhba32)\Split Commands/sec' |
Export-Csv ESX01-Disk.csv -NoType -noClobber
$a | select '(PDH-CSV 4.0) (EDT)(0)','\\F*ESX*\Network
Port(vSwitch*:*:vmnic*)\Packets Transmitted/sec','\\F*ESX*\Network
Port(vSwitch*:*:vmnic*)\MBits Transmitted/sec','\\F*ESX*\Network
Port(vSwitch*:*:vmnic*)\Packets Received/sec','\\F*ESX*\Network
Port(vSwitch*:*:vmnic*)\MBits Received/sec' | Export-Csv ESX01-Network.csv
-NoType -noClobber

"RichS [MVP]" wrote:
Quote:

> Hmmmmmm
>
> That may be a bit more problematic. Could you post the first 6 or 8 lines
> of your source.csv file so that I can see what is actually coming through.
> Also if you could post which fields you actually want to go in which files
>
> Thanks
>
> --
> Richard Siddaway
> All scripts are supplied "as is" and with no warranty
> PowerShell MVP
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Srinivas" wrote:
>
Quote:

> > Thanks Rich for your response.
> > My concern regarding changing the CPU% to CPU.
> >
> > Some of the values that I am ACTUALLY extracting from the source CSV file
> > are as below, I wanted to keep it simple while explaining in my initial
> > request:
> >
> > (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Physical Cpu(*)\% Processor Time,
> > (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Logical Cpu(*)\% Processor Time,
> > (PDH-CSV 4.0) (EDT)(0),\\F*ESX*\Memory\Console MBytes,
> > \\F*ESX*\Memory\Kernel MBytes, \\F*ESX*\Memory\NonKernel MBytes,
> > \\F*ESX*\Memory\Free MBytes, \\F*ESX*\Memory\Kernel Reserved MBytes,
> > \\F*ESX*\Console Memory\Free MBytes, \\F*ESX*\Memory\Kernel Unreserved MBytes
> >
> > So I am wondering if there is a way to modify the script you mentioned with
> > all the back slashes and curly brackets and percentage symbols in the CSV
> > file.
> >
> > Regards
> >
> >
> > "RichS [MVP]" wrote:
> >
Quote:

> > > You could try something like this
> > >
> > >
> > > Import-Csv source.csv | foreach {
> > > Out-File cpu.csv -InputObject "$($_.Time),$($_.CPU)" -Append
> > > Out-File memory.csv -InputObject "$($_.Time),$($_.Memory)" -Append
> > > Out-File network.csv -InputObject "$($_.Time),$($_.Network)" -Append
> > > }
> > >
> > > Export-csv doesn't have an append option which is why I've had to change to
> > > out-file. Notice that I've had to change CPU% to CPU to get this to work
> > >
> > >
> > >
> > >
> > > --
> > > Richard Siddaway
> > > All scripts are supplied "as is" and with no warranty
> > > PowerShell MVP
> > > Blog: http://richardsiddaway.spaces.live.com/
> > > PowerShell User Group: http://www.get-psuguk.org.uk
> > >
> > >
> > > "Srinivas" wrote:
> > >
> > > > Dear all,
> > > > I am trying to read from a CSV file using import-csv cmdlet and trying to
> > > > extract relevant information using export-csv it into 3CSV files(exporting to
> > > > 3 files is mandatory for another process to read them).
> > > > I could not figure out to write in a single line to import the source CSV
> > > > file and extract into 3 or more files so I am doing it in three lines.
> > > >
> > > > Below are more details
> > > > ##########################
> > > > #This is source CSV file
> > > > Time,CPU%,Memory,Network,disk
> > > > 10:00:01,2,2048,100,3
> > > > 10:01:01,3,4096,100,4
> > > > .
> > > > .
> > > > .
> > > > .
> > > > #end of file
> > > > ############################
> > > >
> > > > ps1 script below that I use
> > > > #################################################
> > > > #Script to read the source and write to three different CSV files
> > > > $a = import-csv Source.csv
> > > > $a | select 'Time','CPU*' | Export-Csv cpu.csv -NoType -noClobber
> > > > $a | select 'Time','Memory'| Export-Csv Memory.csv -NoType -noClobber
> > > > $a | select 'Time','Network | Export-Csv Network.csv -NoType -noClobber
> > > > #end of script
> > > > #################################################
> > > >
> > > > Is there a better way to do this job then what I am currently doing?
> > > > Currently it is doing the job but because of number of columns in the source
> > > > file are more than 1000 and the source CSV files are like 25MB it takes 1/2
> > > > for extracting each source CSV file
> > > >
> > > > Appreciate any kind of feedback
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export-Csv multiple hashtable values PShelp PowerShell 2 04-10-2008 03:39 PM
Having all Export files append to 1 file, xls, or csv... j0seg PowerShell 6 02-11-2008 10:58 AM
Speech Recognition Export Files mglacey Vista General 2 03-05-2007 05:23 PM
Re: Not able to EXPORT files into the local system PA Bear Vista General 1 02-19-2007 05:46 AM
Export-CliXml/Export-Csv: Change to Export-Object? Alex K. Angelopoulos [MVP] PowerShell 3 06-04-2006 07:57 PM








Vistax64.com 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 2005-2008

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 47 48 49 50