I want to extract the key value in Countries in the following plist. For example, I want the output file contains the following keys only:
USA ITALY
But if I use the following script, It will print all the nested element under Countries including: Capital Washington DC Continent North America.
How can I modify the script so that it will only print the keys under Countries?
output=$(/usr/libexec/PlistBuddy "$infoplistPath/Info.plist" -c "Print :Nation:Countries")
Here is the plist:
<dict> <key>Nation</key> <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> </dict>
Advertisement
Answer
I couldn’t find a way to do with PlistBuddy
directly, but with some tinkering, I was able to process the output to get what you want:
output=$(/usr/libexec/PlistBuddy "$infoplistPath/Info.plist" -c "Print :Nation:Countries" | grep Dict | sed 's/^(.*) =.*/1/g' | grep -v "Dict {" | awk '{print $1}')
When you echo the contents of $output
it comes back with this:
$ echo $output USA Italy