Reputation: 9295
The get-member commandlet returns NoteProperties always sorted alphanumerically. See sample output below.
From import-csv I received an array in memory, now I want get-member to sort the member names by original position rather than its alphanumerical value.
The original sequence is visible in the $_.psextended.Definition string ( column names joined by commas)
I cannot in-place edit the property names, as it's read-only. As I workaround I tried to prepend a numeric prefix to the column name, see code below
Any better ideas? I don't want to in-place edit the original data file.
$content = (import-csv -delimiter "`t" $infile_abs );
$colnames = $content | get-Member | where-object {$_.MemberType -eq "NoteProperty"} ; #| out-gridview;
$cols = $content | get-Member -force | where-object {$_.Name -eq "psextended"} ;
echo($cols.Definition -replace "psextended" , "");
$i = 0;
$colnames| sort-object -descending | foreach-object {
$i++ ;
$cn = [string]::Format( "{0:00}_{1}", $i, $_.Name ) ;
Write-Host $cn
};
Sample Output of psextended
{File Name, Label, ObsDateTime, Port#, Obs#, Exp_Flux, IV Cdry, IV Tcham, IV Pressure, IV H2O, IV V3, IV V4, IV RH}
Output of $colnames = $content | get-Member | out-gridview;
Exp_Flux NoteProperty System.String Exp_Flux=0,99
File Name NoteProperty System.String File Name=xxx-11040
IV Cdry NoteProperty System.String IV Cdry=406.96
IV H2O NoteProperty System.String IV H2O=9.748
IV Pressure NoteProperty System.String IV Pressure=100.7
IV RH NoteProperty System.String IV RH=53.12
IV Tcham NoteProperty System.String IV Tcham=16.19
IV V3 NoteProperty System.String IV V3=11.395
IV V4 NoteProperty System.String IV V4=0.759
Label NoteProperty System.String Label=5m
Obs# NoteProperty System.String Obs#=1
ObsDateTime NoteProperty System.String ObsDateTime=2011-04-04 13:19:37
Port# NoteProperty System.String Port#=1
EDIT: (No answers yet)
Here is a custom sorting function, now I need to tell Get-Member to use this sorting function. How to do this in a pipeline?
#$orig_seq = $cols.Definition -replace "psextended", "" -replace "[{}]", "";
$orig_seq = "File Name, Label, ObsDateTime, Port#, Obs#, Exp_Flux, IV Cdry, IV Tcham, IV Pressure, IV H2O, IV V3, IV V4, IV RH";
echo $orig_seq;
#exit;
&{
$byPos= @{};
$i = 0;
$orig_seq.Split(",") | % { $byPos[$i++] = $_.trim()}
$order = ([int[]] $byPos.keys) | sort
#$order | %{ ([string]::Format( "{0} => {1}", $_, $byPos[$_])) }
$order | %{ $byPos[$_] }
}
Upvotes: 2
Views: 11287
Reputation: 647
I approached this in a slightly different way when processing a CSV file:
# Create a stream object where $filepath is the CSV file you're processing
$stream = [System.IO.File]::Open($filepath, [System.IO.FileMode]::Open)
# Create a reader of the stream, yes there are more efficient ways to do this
$reader = New-Object -typename System.IO.StreamReader -argumentlist $stream
# Output the first line to the target CSV file you're processing then move on...
$reader.ReadLine() | Out-File $outpath -Encoding "ASCII"
Less 'powershelly', but easy to achieve...
Upvotes: 0
Reputation: 9295
I found the answer in question PSCustomObject to Hashtable
The in-memory data from import-csv cmdlet is a PSCustomObject.
Its properties (column names) can be fetched in the original order with this code
#fetch in correct order
$content.psobject.properties |
# do something with the column names
Foreach { $ht2["$($_.Name)"] = $_.Value }
Properties can be renamed this way, see
http://powershell.com/cs/blogs/tips/archive/2009/05/08/renaming-object-properties.aspx
dir | Select-Object @{Name='FileName'; Expression={$_.Name}}
Upvotes: 1
Reputation: 24826
Just a trace:
$myorder = $orig_seq.split(",") foreach $myobject in $myobjects { $myorder | % {get-member -name $_ -inputobject $myobject} }
EDIT: get sorted values
$myorder = $orig_seq.split(",") foreach $myobject in $myobjects { $mysortedvalues | ? {$myobject.name -eq $_} }
Obviously this is useful and feasable if you now $mysortedvalues in advance.
Upvotes: 0