Skip to content

Instantly share code, notes, and snippets.

@Spitfire1900
Last active August 12, 2022 06:00
Show Gist options
  • Save Spitfire1900/035a859a024c20b76a3a to your computer and use it in GitHub Desktop.
Save Spitfire1900/035a859a024c20b76a3a to your computer and use it in GitHub Desktop.
Parse XML in bash
#! /bin/bash
#xml parser, assigns entity to $E and content to $C
rdom () { local IFS=\> ; read -d \< E C ;} # https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash/7052168#7052168
while rdom; do
if [[ $E = $2 ]]; then
echo $C
exit
fi
done < $1
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>sth-items</Name>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>[email protected]</Key>
<LastModified>2011-07-25T22:23:04.000Z</LastModified>
<ETag>&quot;0032a28286680abee71aed5d059c6a09&quot;</ETag>
<Size>1785</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
</ListBucketResult>
@Spitfire1900
Copy link
Author

$ ./parsexml.sh test.xml Key outputs "[email protected]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment