5

How can I use the output of one command - line-by-line - into another command? I know how to do this with the | pipe symbol, but this uses the entire output in one command. I would like to go line by line... I think I need to combine the | and xargs but not sure.

redis-cli keys \* | redis-cli get [key would go here]
d-_-b
  • 1,177

3 Answers3

9

The xargs answer would be:

redis-cli keys \* | xargs -L 1 redis-cli get
glenn jackman
  • 85,964
5

Use while to loop through each line:

redis-cli-keys \* | while read key; do redis-cli get "$key"; done
laebshade
  • 2,176
  • awesome thank you! is the term read always going to be in those types of commands? – d-_-b Dec 06 '13 at 02:35
  • 1
    read can be used in a lot of instances. For example, as a standalone to take input and assign to a variable (with a prompt): read -p "Enter a name: " name - the value entered will then be assigned to $name. – laebshade Dec 06 '13 at 02:37
  • Very cool, i'll look into that. thanks again - this worked! – d-_-b Dec 06 '13 at 02:40
  • 1
    read is also handy to assign words in a string to variables: x="1 2 3 4 5"; read a b c rest <<< "$x" – glenn jackman Dec 06 '13 at 02:47
4

Just for completeness here's the for loop variant:

$ for key in $(redis-cli-keys \*); do redis-cli get $key; done

NOTE: This works so long as the $keys do not contain spaces.

slm
  • 369,824