16

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?

smokinguns
  • 1,478

1 Answers1

19

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