Pipeable Ruby - forget about grep / sed / awk / wc ...
github.com
github.com
# grep --- all lines including current date
ls -al | ???
ls -al | pru 'include?(Time.now.strftime("%Y-%m-%d"))'
Unless I'm missing something, that would be: ls -al | grep `date +"%Y-%m-%d"` ls -al | grep $(date +%Y-%m-%d) ls -al | grep $(date +%F)I don't quite see the benefit of pru from the examples for 'grep/sed/awk' users, memory and speed issues put aside.
Even the 'number of files by date' can be realized with, eg., awk:
ls -al |awk '{++a[$6]} END {for (i in a) printf "%s: %d\n", i, a[i]}'no thanks, link-bait!
grep/sed/awk are highly specialized programs optimized to do one thing very well.
A tool like this one might pick for reasons other than performance perhaps (maybe you're a Ruby programmer and can't grok grep/sed/awk?)
Check man ruby and you'll see familiar (if you're used to Perl one-liners) command-line switches: -p, -l, -n, -F, -a, -i and so on.
See also here for some familiar friends: http://www.zenspider.com/Languages/Ruby/QuickRef.html#19
http://code.activestate.com/recipes/437932-pyline-a-grep-lik...
Or funcpy:
ps -ef | grep foo | grep -v grep
This always works: ps -ef | grep '[f]oo'
And, if you're running a modern operating system: pgrep fooAnd I almost always want the full ps output, or I'm searching for a commandline argument rather than the process name, so pgrep is out.
function psg () { [[ -z "$1" ]] && return ; local _first=${1:0:1} ; local _rest=${1:1} ; [[ -z "$_rest" ]] && _rest='[^]]' ; local _com="ps -ef | grep '[$_first]'$_rest" ; eval "$_com" } ps -ef | grep foo | grep -v grep
Will fail if foo="grep"I think to make something as instantly usable as the the shell is quite hard, but to make an environment that is suitable for harder tasks that just get messy in the shell in a better language, but which still inherits a shell like way of doing things could be an interesting project.
# seq $((2**32)) | pru "/find me/" >/dev/null
gems/pru-0.1.3/lib/pru.rb:13:in `[]': failed to allocate memory (NoMemoryError)
Guess I'll try to not forget about grep and sed just yet... seq $((2**32)) | perl -ne '/find me/ && print' # --- print second column
ls -al | awk '{print $2}'
ls -al | pru 'split(" ")[1]'
ls -al | scc -n 'F(1)'
# --- count and average of all integers on second position
ls -al | awk '{ s += $2; } END {print "average" ,int(s/NR);print "count ",int(NR)}'
ls -al | pru 'split(" ")[1]' '"average #{mean(&:to_i)}\ncount #{size}"'
ls -al | scc 'int c=0; WRL c+=F(1); FMT("average %s\ncount %s") %(c/NR) %NR'
# --- count lines
ls -al | wc -l
ls -al | pru -r 'size'
ls -al | scc 'WRL;NR+1'
# -- replace a 5 with five
ls -al | sed 's/5/five/'
ls -al | pru 'gsub(/5/,"five")'
ls -al | scc -n 'RR(line,R("5"),"five")'
# every second line
ls -al | pru 'i % 2 == 0'
ls -al | scc -n 'NR % 2 ? line : ""'
# sum up df's used-space column
df | awk '{n+=$3;}; END{print n}'
df | pru ?????
df | scc 'int n=0; WRL n+=F(2); n'rw@raccoon:~> du -h messages
19M messages
rw@raccoon:~> time grep -e "foobar" < messages
real 0m0.030s
user 0m0.022s
sys 0m0.008s
rw@raccoon:~> time pru /foobar/ < messages
real 0m0.796s
user 0m0.722s
sys 0m0.071s
The answer is usually (but not always) developer time.