Last active
March 4, 2022 05:49
-
-
Save thanosa75/00b1c14105f4f42495ab to your computer and use it in GitHub Desktop.
Example of SCAN / COUNT looking for patterns in REDIS
This file contains 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 | |
# sample code demonstrating a pattern search with SCAN | |
# using a COUNT "work intent" | |
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=`redis-cli -h $1 -p $2 SCAN $cursor COUNT 10000 MATCH $3` | |
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'` | |
keys=${reply/$cursor/} | |
if [[ $keys != "" ]] | |
then | |
echo "keys: $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
This is incorrect solution. If keys, returned in
reply
, have substrings, equal tocursor
, these keys will be corrupted by regex replacement on line 25.It is better to use arrays: