Skip to content
hayeah edited this page Mar 8, 2011 · 6 revisions

Say No To Rubbish, Say yes to Rubish!

Feel welcome to add whatever you like. I’d like a pet hippo.

Rubbish to Rubish

example 1
cat file.txt | less

p { cat "file.txt"; less }

example 2
find . -name '*tmp' | xargs rm -r

rm(:r,find(". -name '*tmp'").map)

example 3
grep -ir 'my_name' . | awk -F: '{print $1}'

grep("-ir foo app").awk { puts line.split(":").first }
# or more rubyesque (preferred)
grep("-ir foo app").map { |l| puts l.split(":").first }

General Principles

The `map` method converts a command into an array of output.

Whenever you want to use xargs, just use array of output as the arguments to the command. Like so:

cmd1(cmd2.map)

The arguments you give to a command is recursively normalized, so there’s a lot of flexibility in how the arguments are given. You can mix command output as arguments, as well as manually specifying arguments:

cmd1(cmd2.map,"arg1, arg2",:flag)

So on and so forth.