shell 如何将输出转换为JSON格式?

zz2j4svz  于 2022-11-16  发布在  Shell
关注(0)|答案(1)|浏览(195)

我正在尝试将下面的输出转换为有效的json输出。无法使用shell脚本解决此问题。

输出

svc pts/0        localhost. Tue Apr 28 21:40   still logged in   
svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)    
svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)
...etc

有效的json输出

{
    "Result": [{
        "models": ["svc pts 0 localhost.Tue Apr 28 21: 40 still logged in",
            "svc pts/0        localhost. Tue Apr 28 21:40 - 21:40  (00:00)"
        ]
    }]
}

是否可以转换?有人能提供解决方案吗?

aij0ehis

aij0ehis1#

您可以使用jq,例如:

program | jq -Rn '{Result:[{models:[inputs]}]}'

program是产生该输出的程序。

相关问题