kubernetes 我们可以在终端中格式化命令kubeadm version的结果吗?[已关闭]

wnavrhmk  于 2023-01-29  发布在  Kubernetes
关注(0)|答案(2)|浏览(86)

**已关闭。**此问题为not about programming or software development。当前不接受答案。

此问题似乎与a specific programming problem, a software algorithm, or software tools primarily used by programmers无关。如果您认为此问题与another Stack Exchange site的主题有关,您可以留下评论,说明在何处可以回答此问题。
昨天关门了。
Improve this question
命令

root@controlplane:~$ kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:57:06Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
root@controlplane:~$ ^C
root@controlplane:~$

我要这样的结果

{
   "Major":"1",
   "Minor":"26",
   "GitVersion":"v1.26.0",
   "GitCommit":"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d",
   "GitTreeState":"clean",
   "BuildDate":"2022-12-08T19:57:06Z",
   "GoVersion":"go1.19.4",
   "Compiler":"gc",
   "Platform":"linux//amd64"
}

wwtsj6pe

wwtsj6pe1#

您可以使用kubeadm具有的选项。

$ kubeadm version -o json
{
  "clientVersion": {
    "major": "1",
    "minor": "20",
    "gitVersion": "v1.20.4",
    "gitCommit": "e87da0bd6e03ec3fea7933c4b5263d151aafd07c",
    "gitTreeState": "clean",
    "buildDate": "2021-02-18T16:09:38Z",
    "goVersion": "go1.15.8",
    "compiler": "gc",
    "platform": "linux/amd64"
  }
}

如果您希望输出与上面完全相同,可以使用jq

$ kubeadm version -o json | jq '.clientVersion'
{
  "major": "1",
  "minor": "20",
  "gitVersion": "v1.20.4",
  "gitCommit": "e87da0bd6e03ec3fea7933c4b5263d151aafd07c",
  "gitTreeState": "clean",
  "buildDate": "2021-02-18T16:09:38Z",
  "goVersion": "go1.15.8",
  "compiler": "gc",
  "platform": "linux/amd64"
}
ncecgwcz

ncecgwcz2#

您可以使用sed来根据您的要求格式化输出。

$ kubeadm version | sed -e 's/{/\n{\n\t /g' -e 's/,/,\n\t/g' -e 's/}/\n}/g' | tail -n +2

结果

{
         Major:"1",
         Minor:"23",
         GitVersion:"v1.23.4",
         GitCommit:"e6c093d87ea4cbb530a7b2ae91e54c0842d8308a",
         GitTreeState:"clean",
         BuildDate:"2022-02-16T12:36:57Z",
         GoVersion:"go1.17.7",
         Compiler:"gc",
         Platform:"linux/amd64"
}

说明:
sed可用于在字符串/文件/STDOUT中搜索和替换。
sed 's/search_parameter/replace_parameter/g'中,s表示搜索,g表示全局替换(如果未指定g,则只替换匹配的搜索项的第一个示例),-e允许您在单个sed命令中指定多个sed命令/脚本。
下面是对上述命令的分解:
-e 's/{/\n{\n\t /g'-搜索{并将{替换为{,然后添加一个新行和制表符。
-e 's/,/,\n\t/g'-搜索并替换为,后跟新行和制表符。
-e 's/}/\n}/g'-搜索}并将}替换为新行,后跟}。
tail -n +2-它将打印除第一行之外的所有内容。

相关问题