13

I have a plist file and I'm interested in fetching values from nested dictionaries using shell script. Here is the structure of my plist file:

<dict>
    <key>Countries</key>
    <dict>
        <key>USA</key>
        <dict>
            <key>Capital</key>
            <string>Washington DC</string>
            <key>Continent</key>
            <string>North America</string>
        </dict>
        <key>Italy</key>
        <dict>
            <key>Capital</key>
            <string>Rome</string>
            <key>Continent</key>
            <string>Europe</string>
        </dict>
    </dict>
</dict>

I'm interested in fetching value assigned to the "Capital" key if user supplies a Country name.
For e.g If I execute the following command,I get the complete data under the "Countries" dictionary.

defaults read "/Users/sameerp/Downloads/data" Countries  2>&1   

I get the following output:

{
    Italy = {
        Capital = Rome;
        Continent = Europe;
    };
    USA = {
        Capital = "Washington DC";
        Continent = "North America";
    };
}

How do I modify the command above to pass it the "Country" Key(USA for e.g.) and fetch the value for "Capital" Key?

2
  • So, what is your question? What do you have so far? You're not asking us to write the script for you right?
    – terdon
    Oct 12, 2012 at 0:01
  • 1
    @terdon: I'm not asking anyone to write the script for me. I wanted to know if there is a direct way to access a key's value in nested dictionary using "defaults read" command.
    – smokinguns
    Oct 12, 2012 at 0:15

1 Answer 1

16

defaults can't do this without a lot of mess, but PlistBuddy will:

/usr/libexec/PlistBuddy -c "print :Countries:USA:Capital" /Users/sameerp/Downloads/data.plist

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .