Skip to content

Instantly share code, notes, and snippets.

@obscurerichard
Forked from itamarhaber/scan_del.sh
Last active March 13, 2023 09:17
Show Gist options
  • Save obscurerichard/f76302a0ad8ab9b20516 to your computer and use it in GitHub Desktop.
Save obscurerichard/f76302a0ad8ab9b20516 to your computer and use it in GitHub Desktop.

Revisions

  1. obscurerichard revised this gist Jan 9, 2020. 1 changed file with 23 additions and 6 deletions.
    29 changes: 23 additions & 6 deletions redis-scan.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,33 @@
    #!/bin/bash
    #!/usr/bin/env bash
    # redis-scan.sh
    #
    # Adapted by @obscurerichard from itamarhaber/scan_del.sh:
    # https://gist.github.com/itamarhaber/11126830
    #
    # Thanks @czerasz and @tenlee2012 for fixes
    #
    # Usage:
    # ./redis-scan.sh localhost 6378 0 '*test*'
    #
    # NOTE: if your redis-cli supports '--scan' use that instead, thanks @ferico
    #
    # redis-cli --scan --pattern '*'
    #

    if [ "$#" -lt 2 ]
    if [ "$#" -lt 2 ]
    then
    echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
    echo "Usage: $0 <host> [port] [database] [pattern]"
    exit 1
    fi
    host=${1:-}
    port=${2:6379}
    port=${2:-6379}
    database=${3:-0}
    pattern=${3:-\*}
    pattern=${4:-\*}
    cursor=-1
    keys=""

    echo "host=${host},port=${port},database=${database}"
    while [[ "$cursor" -ne 0 ]]; do
    if [[ "$cursor" -eq -1 ]]
    then
    @@ -21,6 +36,8 @@ while [[ "$cursor" -ne 0 ]]; do

    reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern")
    cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
    keys=${reply##[0-9]*[0-9 ]}
    echo $keys
    keys=${reply//$cursor/}
    if [ -n "$keys" ]; then
    echo "$keys"
    fi
    done
  2. obscurerichard revised this gist Sep 17, 2015. 2 changed files with 26 additions and 23 deletions.
    26 changes: 26 additions & 0 deletions redis-scan.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/bin/bash

    if [ "$#" -lt 2 ]
    then
    echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
    echo "Usage: $0 <host> [port] [database] [pattern]"
    exit 1
    fi
    host=${1:-}
    port=${2:6379}
    database=${3:-0}
    pattern=${3:-\*}
    cursor=-1
    keys=""

    while [[ "$cursor" -ne 0 ]]; do
    if [[ "$cursor" -eq -1 ]]
    then
    cursor=0
    fi

    reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern")
    cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
    keys=${reply##[0-9]*[0-9 ]}
    echo $keys
    done
    23 changes: 0 additions & 23 deletions scan_del.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    #!/bin/bash

    if [ $# -ne 3 ]
    then
    echo "Delete keys from Redis matching a pattern using SCAN & DEL"
    echo "Usage: $0 <host> <port> <pattern>"
    exit 1
    fi

    cursor=-1
    keys=""

    while [ $cursor -ne 0 ]; do
    if [ $cursor -eq -1 ]
    then
    cursor=0
    fi

    reply=`redis-cli -h $1 -p $2 SCAN $cursor MATCH $3`
    cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
    keys=${reply##[0-9]*[0-9 ]}
    redis-cli -h $1 -p $2 DEL $keys
    done
  3. @itamarhaber itamarhaber created this gist Apr 20, 2014.
    23 changes: 23 additions & 0 deletions scan_del.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #!/bin/bash

    if [ $# -ne 3 ]
    then
    echo "Delete keys from Redis matching a pattern using SCAN & DEL"
    echo "Usage: $0 <host> <port> <pattern>"
    exit 1
    fi

    cursor=-1
    keys=""

    while [ $cursor -ne 0 ]; do
    if [ $cursor -eq -1 ]
    then
    cursor=0
    fi

    reply=`redis-cli -h $1 -p $2 SCAN $cursor MATCH $3`
    cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
    keys=${reply##[0-9]*[0-9 ]}
    redis-cli -h $1 -p $2 DEL $keys
    done