Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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 '12 at 0:01
    
@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 '12 at 0:15
    
+1, thanks for the update. –  terdon Oct 12 '12 at 1:17
add comment

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.