首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在powershell中选择具有特殊字符的键?

如何在powershell中选择具有特殊字符的键?
EN

Stack Overflow用户
提问于 2019-04-19 21:33:38
回答 2查看 929关注 0票数 3

我有一组来自JSON文件的键/值对。

代码语言:javascript
复制
$ $p.dependencies

@architect/architect : ^5.7.0
@architect/functions : ^3.0.4
assert               : ^1.4.1
bcrypt               : ^3.0.6
find-parent-dir      : ^0.3.0
hashids              : ^1.2.2
http-status-codes    : ^1.3.2
lodash.get           : ^4.4.2
mini-web-server      : ^1.0.2
mini-webhook-server  : ^1.0.4
mocha                : ^5.2.0
opencorporates       : ^3.0.0
stripe               : ^6.23.1
uuid                 : ^3.3.2
whois-json           : ^2.0.4

在Powershell中,它们是noteProperty的:

代码语言:javascript
复制
   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()
@architect/architect NoteProperty string @architect/architect=^5.7.0
@architect/functions NoteProperty string @architect/functions=^3.0.4
assert               NoteProperty string assert=^1.4.1
bcrypt               NoteProperty string bcrypt=^3.0.6
find-parent-dir      NoteProperty string find-parent-dir=^0.3.0
hashids              NoteProperty string hashids=^1.2.2
http-status-codes    NoteProperty string http-status-codes=^1.3.2
lodash.get           NoteProperty string lodash.get=^4.4.2
mini-web-server      NoteProperty string mini-web-server=^1.0.2
mini-webhook-server  NoteProperty string mini-webhook-server=^1.0.4
mocha                NoteProperty string mocha=^5.2.0
opencorporates       NoteProperty string opencorporates=^3.0.0
stripe               NoteProperty string stripe=^6.23.1
uuid                 NoteProperty string uuid=^3.3.2
whois-json           NoteProperty string whois-json=^2.0.4

我想用@architect/functions ^3.0.4 键将项更改为与^3.0.4(然后将数据保存回来)不同的值

接下来的部分似乎是从哈希表中选择正确的项。我正在使用:

代码语言:javascript
复制
$p.dependencies | where $_.key -eq "@architect/functions"

但是,这不会返回任何结果。如何选择带有键@architect/functions**?**的项目作为积分,如何更改值!

编辑:

使用这个答案,这是我的最后一个脚本,如果有人觉得有用的话。

代码语言:javascript
复制
ls 'src/http' | foreach { 
    $packageJSONFile = "${PSItem}\package.json" 
    $packageJSON = cat $packageJSONFile | convertfrom-json
    if ( $packageJSON.dependencies.'@architect/functions'  ) {
        $packageJSON.dependencies.'@architect/functions' = '^3.0.4'
    }   
    $packageJSON | ConvertTo-Json -depth 100| set-content $packageJSONFile
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-19 21:44:01

若要获取名称中具有特殊字符的属性,请引用该属性:

代码语言:javascript
复制
$p.dependencies.'@architect/functions'
票数 5
EN

Stack Overflow用户

发布于 2019-04-20 03:09:43

Tomalak's helpful answer解决了你的问题。

至于您试图使用where (Where-Object)检索感兴趣的属性:

$p.dependencies | where $_.key -eq "@architect/functions"

有两个基本问题:

  • 在摘要中,使用比较语句语法(即使用单个参数而不是脚本块)要求您单独使用属性名称(key),而不是通过自动变量$_ ($_.key)。
代码语言:javascript
复制
- In fact, `$_` - to refer to the pipeline input object at hand - is only ever meaningful _inside a script block_ (`{ ... }`).
- That is, if your command were otherwise correct (it isn't, see below), you'd have to use:

$p.dependencies | where key -eq '@architect/functions'

  • 如您的输出所示,$p.dependencies包含一个[pscustomobject]实例,而不是哈希表([hashtable]);ConvertFrom-Json返回[pscustomobject]实例,除非您使用-AsHashtable开关显式请求哈希表。
代码语言:javascript
复制
- If it were a hash table, PowerShell would send it through the pipeline _as a whole_.
- In order to send a hash table's _entries_ one by one through the pipeline, you'd have to call `.GetEnumerator()` on it, which would allow you to filter by their `Key` property.

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55768097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档