我有一个很大的 YAML 文件,其中包含有关视频游戏的信息。
---
-
Name: Metal Gear Solid HD Collection
Console: PS3
Genre: Stealth
Progression: Unplayed
Maintenance: Perfect!
-
Name: Metal Gear Solid 4 Guns of the Patriots
Console: PS3
Genre: Stealth
Progression: Unplayed
Maintenance: Perfect!
-
Name: Metal Gear Rising Revengeance
Console: PS3
Genre: Power Fantasy
Progression: Compleat (Blade Wold DLC to play)
Maintenance: Perfect!
-
我正在尝试将大文件拆分为每个游戏一个文件。我找到了一个很好的解决方案,使用:
yq e GameCollection.yaml -s .Name
但这会出现以下错误
Error: cannot index array with 'Name' (strconv.ParseInt: parsing "Name": invalid syntax)
我假设这是因为它试图将 Name 视为字符串,所以我用单引号将其括起来以使其更像字符串。我在这里找到了这个解决方案(在获得最多支持的答案下的评论中),它让我看到了不同的错误消息并解决了这个问题。
yq e GameCollection.yaml -s '.Name'
这给了我一个不同的错误,我认为这是因为名称不是唯一标识符,因为有时多个控制台上有相同的游戏(例如 Skyrim)
Error: bad split document expression: 1:1: invalid input text "'.Name'"
我发现,我认为一个很好的解决方案是添加索引,这样重复的名称就可以增加!我在这里找到了解决方案。
yq e GameCollection.yaml -s '.Name + "_" + $index'
但它给了我类似的错误。
Error: bad split document expression: 1:1: invalid input text "'.Name"
我又试了几次,因为我认为我只是不明白语法。
yq e GameCollection.yaml -s '.Name' + "_" + '$index'
Error: bad split document expression: 1:1: invalid input text "'.Name'"
yq e GameCollection.yaml -s '.Name' + "_" + $index
Error: bad split document expression: 1:1: invalid input text "'.Name'"
yq e GameCollection.yaml -s .Name + "_" + '$index'
Error: cannot index array with 'Name' (strconv.ParseInt: parsing "Name": invalid syntax)
这几乎就像它不喜欢单引号?
有人可以给我指明正确的方向吗?