Home > database >  Can we format result of command kubeadm version in terminal?
Can we format result of command kubeadm version in terminal?

Time:01-27

command

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:~$

I want result like this

{
   "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"
}

enter image description here

CodePudding user response:

you can use an option that kubeadm has.

$ 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"
  }
}

If you want the output to be exactly like above, it is possible using 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"
}

CodePudding user response:

You can use sed to format the output as per your requirement.

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

Result

{
         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"
}
  • Related