Reputation: 5
$s = import-csv AAPL.csv
$s | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Adj Close NoteProperty System.String Adj Close=103.739998
Close NoteProperty System.String Close=103.739998
Date NoteProperty System.String Date=2015-08-03
High NoteProperty System.String High=122.57
Low NoteProperty System.String Low=92.00
Open NoteProperty System.String Open=121.50
Volume NoteProperty System.String Volume=82932100
# $ts = $s.'Adj Close' //OK
# $ts = $s[ 0 .. $s.Count][3] //Anyway of doing this? suppose 'Adj Close' is on column 4.
I am wondering if I can get NoteProperty value by a positional number/id.
I am thinking this because I may encounter financial data that do not have the exact name as 'Adj Close' , such as 'Last Close', 'Close', 'Adjusted Close' etc... on the same column and that can be analyzed using the same code.
Thank you!
Upvotes: 0
Views: 388
Reputation: 24071
In order to select CSV columns with fuzzy names, you can use a regex to select the column names.
As for a working sample, let's pick some columns:
# Test data
$data = @'
"Last Close", "Close", "Adjusted Close", "Other Close"
10, 20, 30, 40
50, 60, 70, 80
'@
# Convert here-string into csv so it behaves like import-csv's output
$csvdata = convertfrom-csv $data
# Match only Close header by looking at headers and picking just it
[string[]]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "^Close$"} |
select -expand Name
$csvdata | select $headers
Close
-----
20
60
# Match Last or Adjusted Close with same technique
[string[]]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "^((Last)|(Adjusted)) Close$"} |
select -expand Name
$csvdata | select $headers
Adjusted Close Last Close
-------------- ----------
30 10
70 50
Upvotes: 1