I'd like to run a shell command on each line taken from STDIN.
In this case, I'd like to run xargs mv. For example, given two lines:
mfoo foo
mbar bar
I'd like to run:
xargs mv mfoo foo
xargs mv mbar bar
I've tried the following strategies with ruby, awk, and xargs. However, I'm doing it wrong:
Just xargs:
$ echo "mbar bar\nmbaz baz" | xargs mv
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
Through awk:
$ echo "mbar bar\nmbaz baz" | awk '{ system("xargs $0") }'
Through ruby:
$ echo "mbar bar\nmbaz baz" | ruby -ne '`xargs mv`'
$ ls
cat foo mbar mbaz
I have some questions:
- How do I do what I'm trying to do?
- What is wrong with each of my attempts?
- Is there a better way to "think about" what I'm trying to do?
I'm especially confused that my xargs attempt isn't working because the following works:
$ echo "foo\nbar" | xargs touch
$ ls
bar foo