PowerShell Core 不能很好地与 jq 配合使用

PowerShell Core not playing nicely with jq

我正在尝试从 git 存储库中获取所有分支并将它们存储在 PSObject 中。我的代码在 zsh 和 bash 中有效,但在 PowerShell Core 中无效。查看 PowerShell 文档 (Quoting Rules, and Special Characters),我看不到任何需要转义的内容。由于该行在 zsh 和 bash 中有效,我认为 jq 不是问题所在。

命令中失败的部分是:

jq --raw-input --slurp 'split("\n") | map(split("\t")) | .[0:-1]'

错误是:

jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
split(\n) | map(split(\t)) | .[0:-1]
jq: 1 compile error

我正在使用的整个命令是(在 bash 中将 ` 换行符替换为 \ ):

git for-each-ref --sort='-authordate:iso8601' `
    refs/heads `
    --format='%(refname:short)%09%(creatordate:rfc)%09%(creator)%09%(committerdate:rfc)%09%(committeremail)' `
    | jq --raw-input --slurp 'split("\n") 
      | map(split("\t")) 
      | .[0:-1] 
      | map( {
        "branchName": .[0], 
        "AuthorDate": .[1], 
        "Author": .[2], 
        "CommitDate": .[3], 
        "Committer": .[4]
      })'

我的 PowerShell Core 版本是: 6.2.1 我是来自 iTerm2 的 Mac 的 运行。我通过 pwsh.

调用 PowerShell

根据错误信息,双引号被去掉了。因此,

split("\n") | map(split("\t")) | .[0:-1]

变成

split(\n) | map(split(\t)) | .[0:-1]

编辑:根据评论,在这种情况下,将双引号加倍即可。没有使用反引号转义:

split(""\n"") | map(split(""\t"")) | .[0:-1]

通常值得尝试 Powershell 的转义字符,反引号 `,像这样转义双引号,

split(`"\n`")

(在这种特殊情况下不起作用。Shell 转义可能很棘手。)