Skip to content

Instantly share code, notes, and snippets.

@brennovich
Created November 20, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennovich/093d6885661383487e162aca4f0a50ce to your computer and use it in GitHub Desktop.
Save brennovich/093d6885661383487e162aca4f0a50ce to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Simple wrapper of Redis' SCAN command. It can control batch size, patterns and cursors.
if [ $# -ne 3 ]
then
echo "Find matching a pattern using SCAN "
echo "Usage: $0 <host> <port> <pattern>"
exit 1
fi
cursor=-1
keys=""
loops=0
while [ $cursor -ne 0 ]; do
if [ $cursor -eq -1 ]
then
cursor=0
fi
reply=`docker run -it --rm --net=host redis:alpine redis-cli -h $1 -p $2 SCAN $cursor COUNT 10000 MATCH $3`
cursor=$(echo "$reply" | head -n1 | grep -o '[0-9]*[0-9]\{2,\}')
keys=$(echo "$reply" | tail -n $((($(echo "$reply" | wc -l) - 1))) | awk '{ print $2 }' | grep -oP '(?<=").*(?=")')
if [[ $keys != "" ]]
then
echo "$keys"
fi
loops=$(( loops + 1 ))
done
echo "loops : $loops"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment