redis-cli查询及配合awk变量处理key
使用redis-cli命令直接查询key
redis-cli -h 192.168.23.121 -p 6379 -a 123456 keys "*"
使用redis-cli命令配合awk变量处理key
1)新建clientinfo.txt,内容:
37020240506001 123902
37020240506002 372348
2)准备redis数据,内容:
set CLIENT37020240506001 哈哈哈
set CLIENT37020240506002 哈哈哈
set CLIENT37020240506003 哈哈哈
set CLIENT37020240506004 哈哈哈
3)执行下面的命令
redis-cli -h 192.168.23.121 -p 6379 -a 123456 keys "CLIENT*" | grep -Ev '^[A-Za-z]+$' | awk -v file=./clientinfo.txt 'BEGIN{while((getline<file)>0) expires[$1]=$2}{id=gensub(/[A-Z]+([0-9]+)/,"\\1",1);time=expires[id];if(length(time)>0) print "expire "$0" "time; else print "del "$0;}' | sed 's/$/\r/g' > rediscmd.txt
4)查看执行结果
cat rediscmd.txt
del CLIENT37020240506004
del CLIENT37020240506003
expire CLIENT37020240506001 123902
expire CLIENT37020240506002 372348