Created
May 27, 2015 00:16
-
-
Save BrianAdams/9ac37409399b6b5123e0 to your computer and use it in GitHub Desktop.
Given a regex patten, parses the feed from and S3 XML listing from the bucket and finds the file matching the pattern with the most recent date.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
read_dom () { | |
local IFS=\> | |
read -d \< ENTITY CONTENT | |
local ret=$? | |
TAG_NAME=${ENTITY%% *} | |
ATTRIBUTES=${ENTITY#* } | |
return $ret | |
} | |
i=0; | |
declare -a parsed | |
parse_dom () { | |
if [[ $TAG_NAME = "Key" ]] ; then | |
eval local $ATTRIBUTES | |
parsed[0]="$CONTENT" | |
IFS='/' read -ra ADDR <<< "$CONTENT" | |
parsed[1]=${ADDR[1]} | |
i=2 | |
elif [[ $TAG_NAME = "LastModified" ]] ; then | |
eval local $ATTRIBUTES | |
parsed[2]="$CONTENT" | |
i=3 | |
fi | |
} | |
result='2010-08-01T18:40:59.000Z' | |
while read_dom; do | |
parse_dom | |
if [ $i == 3 ] ; then | |
i=0 | |
if [[ '${parsed[1]}' =~ $1 ]] ; then | |
todate=$(date -d "${parsed[2]}" "+%s" ) | |
cond=$(date -d "$result" "+%s") | |
if [ ${todate} -ge ${cond} ]; | |
then | |
result=${parsed[2]} | |
ret=${parsed[0]} | |
fi | |
fi | |
fi | |
done | |
echo $ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment