Dhananjay Gahiwade
Dhananjay Gahiwade

Reputation: 359

How to print only name in shell script

listofallvms =
[
    [
        {
            "Name": "aaa"
        }
    ],
    [
        {
            "Name": "bbb"
        }
    ],
]

From the above data, I want to display only data of Name as follows:

aaa
bbb

How to do that any idea?

Currently, I am using below code.

for i in "${listofallvms[0]}"; 
do 
   echo $i | jq .[].[] 
done

Upvotes: 0

Views: 245

Answers (2)

Vvojtek
Vvojtek

Reputation: 43

I would do this like that in your case

grep Name yourfile | awk '{print $2}' | cut -d "\"" -f 2

And the output is

aaa
bbb

Try this.

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133600

With your shown samples please try following jq code. Where listofallvms is your shell variable which is being passed to jq command as an input. Also your variable was having extra comma which was breaking json format so I had fixed it below.

##Shell variable here.
listofallvms='[
    [
        {
            "Name": "aaa"
        }
    ],
    [
        {
            "Name": "bbb"
        }
    ]
]'

jq code here: Using jq with its -r option to get output in raw format.

jq -r '.[][].Name' <<<"$listofallvms"

Upvotes: 2

Related Questions