开发者

Powershell < symbol in property name

I am processing a csv file in powershell which looks like this:

<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>开发者_开发问答;,<HIGH>,<LOW>,<CLOSE>,<VOL>
AUDJPY,20010102,230100,64.30,64.30,64.30,64.30,4
AUDJPY,20010102,230300,64.29,64.29,64.29,64.29,4
<snip>

I am using Import-Csv to process the file and I want to find return <DTYYYYYMMDD> values greater than 20110101. I tried this:

foreach($file in ls $PriceFolder\*.txt) {Import-Csv $file | Where-Object {$_.<DTYYYYMMDD> -ge 20110101}}

but I get this error

Missing property name after reference operator. I presume it's because of the < symbol? How can I solve this?


1) Use {} to enclose not standard field names (or store them in a variable and refer as $_.$var); 2) Cast the value to [int] in order to avoid issues (data are read as strings from .csv; in this example this is not a problem perhaps but..)

This works:

Import-Csv test.csv | Where-Object {[int]$_.{<DTYYYYMMDD>} -ge 20010102}


You can quote the property name:

$_.'<DTYYYYMMDD>'


Just as a comment on MrKWatkins' answer, in one case single quotes work but double quotes don't work (in version 2.0 at least):

Single Quote:

(dir | Select-Object {$_.Name})[0].'$_.Name'       # GOOD: correctly gives name

Double Quote:

(dir | Select-Object {$_.Name})[0]."$_.Name"       # BAD: returns nothing

Edit: Double Quotes allow variable expansion whereas single quotes do not. For more details see this answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜