8

I've written redis-cli bash script to process all key and value for bulk retrieval but values are not printing as expected. When I give my key in redis-cli its printing with all special characters:

My Key and output from redis-cli

redis-cli MGET "0124" "0016"
1) "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\x00\x1d\x8e1\x0e\xc3@\b\x04?\x04\x0e8Q\x17\xa9\xf9\xdc\xdeY\x0b2\x91[\xfd>K\x99\xfd\xaf\xfc\x03\xeb-1\x10\xef\x00\x00\x00"
2) "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\x00\x1d\x8e1\x92\x031\b\x04?\x04k\x84\x10\xa0\xf8\x1;\xa8-7\xbb\xa2> \xc0n\xdc\xe1\xce\xdb\xbdk\xac\x81\x9a]Q*\x8ex\xa4\xe0\x99\xd5\xd1\xb3\x94w^\x9f]\xa7$2\xce;\xdcp\x9c\x9b\xff;\xff\x01\xb3\xcc\xd5H\xf0\x00\x00\x00"

Can someone please help how to decode this value in redis-cli or shell script. I'm new to Redis - it would be really appreciated if you can help me to fix this issue

I used gunzip - but I'm getting below error:

redis-cli -h GET "100" | gunzip

Error:

   gzip: stdin: unexpected end of file
   gzip: stdin: decompression OK, trailing garbage ignored

Redis-cli code:

#!/bin/sh
hostName=$1
port=$2
count=$3
cursor=-1
keys=""
recordCount=0
while [ $cursor -ne 0 ];
do
        if [ $cursor -eq -1 ]
        then
        cursor=0
    fi
    reply=`redis-cli -h $hostName -p $port SCAN $cursor MATCH "*" COUNT $count`
    cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
    keys=${reply#[0-9]*[[:space:]]}
    value=$(redis-cli -h $hostName -p $port MGET $keys)
    temCount=`echo $value | awk -F\| '{print NF-1}'`
    recordCount=`expr ${temCount} + ${recordCount}`
done
echo "Total no. of documents are: " $recordCount

My Redis key-value pattern:

Keys - 123.item.media
Values - 93839,abc,98,829|38282,yiw,282,282|8922,dux,382,993|

Keys - 234.item.media
Values - 2122,eww,92,211|8332,uei,902,872|9039,uns,892,782|

Keys - 839.item.media
Values - 7822,nkp,77,002|7821,mko,999,822|
learn java
  • 231
  • 3
  • 14
  • 1
    echo -e "..." should do the trick. – Mr. bug Oct 29 '17 at 04:48
  • I tried something like this value=$(redis-cli MGET $keys) echo -e $value | gunzip . But it didn't work – learn java Oct 29 '17 at 05:04
  • What about `value=echo -e "$(redis-cli MGET $keys)" | gunzip` ? – Mr. bug Oct 29 '17 at 05:09
  • I'm getting -e: command not found – learn java Oct 29 '17 at 05:16
  • sorry, `value=$(echo -e "$(redis-cli MGET $keys)" | gunzip)` – Mr. bug Oct 29 '17 at 05:17
  • It says gzip: stdin: unexpected end of file :( – learn java Oct 29 '17 at 05:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157728/discussion-between-learn-java-and-mr-bug). – learn java Oct 29 '17 at 05:46
  • @Mr.bug, `echo` is not *permitted to have* `-e` do anything but print the two-character sequence `-e` on output by the POSIX standard (this is one of very few places where bash is actually doing something the standard prohibits, rather than putting extensions in undefined space), and in some modes (when both `xpg_echo` and `posix` flags are set), bash *doesn't* offer said extension. Consider making a habit of using `printf '%b\n' "$value"` instead. See also http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html, particularly the APPLICATION USAGE and RATIONALE sections. – Charles Duffy Oct 29 '17 at 16:03

0 Answers0