Skip to content

Instantly share code, notes, and snippets.

@brunerd
Created September 30, 2024 18:48
Show Gist options
  • Save brunerd/18ec4b743716c680adfdf448faf4666f to your computer and use it in GitHub Desktop.
Save brunerd/18ec4b743716c680adfdf448faf4666f to your computer and use it in GitHub Desktop.
Get key names (top level or all) from a plist
file="example.plist"
#ensures plist data is ASCII not binary
PLISTDATA=$(defaults export "${file}" -)
## get TOP LEVEL key names from a plist
#xmllint method .007s
#variable
xmllint --xpath "/plist/dict/key/text()" - <<< "${PLISTDATA}"
#file (if not binary plist)
xmllint --xpath "/plist/dict/key/text()" "${file}"
#xpath method .06s
#variable
xpath -e "/plist/dict/key/text()" <<< "${PLISTDATA}" 2>/dev/null
#file
xpath -e "/plist/dict/key/text()" "${file}" 2>/dev/null
## get ALL key names from a plist
#xmllint method .007s
#variable
xmllint --xpath "//key/text()" - <<< "${PLISTDATA}"
#file (if not binary plist)
xmllint --xpath "//key/text()" "${file}"
#xpath method .06s
#variable
xpath -e "//key/text()" <<< "${PLISTDATA}" 2>/dev/null
#file
xpath -e "//key/text()" "${file}" 2>/dev/null
#grep/sed method for simple plist xml .011s
#variable
grep -o '<key>[^<]*</key>' <<< "${PLISTDATA}" | sed -e 's/<key>//g' -e 's/<\/key>//g'
#file (if not binary plist)
grep -o '<key>[^<]*</key>' "${file}" | sed -e 's/<key>//g' -e 's/<\/key>//g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment