81

I have a long text file of redis commands that I need to execute using the redis command line interface:

e.g.

DEL 9012012
DEL 1212
DEL 12214314

etc.

I can't seem to figure out a way to enter the commands faster than one at a time. There are several hundred thousands lines, so I don't want to just pile them all into one DEL command, they also don't need to all run at once.

Shubham
  • 2,847
  • 4
  • 24
  • 37
LTME
  • 1,086
  • 1
  • 10
  • 13

5 Answers5

104

the following code works for me with redis 2.4.7 on mac

./redis-cli < temp.redisCmds

Does that satisfy your requirements? Or are you looking to see if there's a way to programmatically do it faster?

Brad
  • 159,648
  • 54
  • 349
  • 530
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
71

If you don't want to make a file, use echo and \n

echo "DEL 9012012\nDEL 1212" | redis-cli
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
  • Worked like a charm for me! – FearlessFuture Jun 11 '15 at 22:22
  • 23
    On some OS you should use `echo -e` to make OS interpret `\n` as expected. Remember, you can always change default redis dilimited using `redis-cli -d ` – Kirzilla Dec 04 '15 at 19:38
  • 2
    on CentOS, `echo -e "DEL 9012012\nDEL 1212" | redis-cli` – Hao Tan Jul 08 '16 at 07:02
  • 1
    Or, if you're on bash: `redis-cli <<<$'DEL 9012012\nDEL 1212'`. You can also instead of `$''`, just use double quotes and actual new lines (i.e. not `\n`) to separate commands. – Guss Dec 09 '19 at 14:17
21

The redis-cli --pipe can be used for mass-insertion. It is available since 2.6-RC4 and in Redis 2.4.14. For example:

cat data.txt | redis-cli --pipe

More info in: http://redis.io/topics/mass-insert

mrnovalles
  • 353
  • 3
  • 7
  • 1
    If you want to execute multiple commands, e.g. select # and flushdb, then use ControlAltDel's solution because it won't work using the --pipe option. – thdoan Jul 18 '14 at 04:03
  • 1
    // , Is there a way to use the `--pipe` option that would allow it to work for this question? – Nathan Basanese Dec 10 '15 at 10:09
  • I was able to use the `--pipe` option to do a mass set of `EXPIRE` commands, so it works for things other than insertion. Note: I had to make sure each command in the file was separate by both a carriage return _and_ a linefeed, not _just_ a linefeed. – Nathan May 16 '17 at 17:50
9

I know this is an old old thread, but adding this since it seems missed out among other answers, and one that works well for me.

Using heredoc works well here, if you don't want to use echo or explicitly add \n or create a new file -

redis-cli <<EOF
select 15
get a
EOF
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
0

I want to place it here since I searched for my case and found this question along the way. I needed to execute redis multi-line command via npm scripts with variables to be parsed in redis commands (redis implemented as a Docker container). To do so, I created txt named redis-delete-key.txt file with commands:

SELECT 1
DEL ${KEY}

where I choose desired redis db and remove the key.

In package.json I added the following command:

"scripts": {
  ...
  "delete-key-from-redis": "envsubst <redis-delete-key.txt | docker exec -i redis redis-cli"
}

where envsubst is linux command to replace variables in file.

To pass the variables I execute the npm command like this:

KEY="anykey" npm run delete-key-from-redis

Works for me, hope will be helpful for someone.

rkosolapov
  • 11
  • 4