Input file:
$ cat foo
qwe
123
bar
Reverse file lines:
$ ruby -e 'File.open("foo").each_line { |l| puts l.chop.reverse }'
ewq
321
rab
Reverse lines from stdout:
cat foo | ruby -e 'while s = gets; puts s.chop.reverse; end'
Reverse both (ARGF == $<
):
cat foo | ruby -e 'ARGF.each_line { |l| puts l.chop.reverse }'
ruby -e 'ARGF.each_line { |l| puts l.chop.reverse }' foo
Let Ruby iterate for you:
ruby -ne 'puts $_.chop.reverse' foo
Let Ruby print for you (clumsy):
ruby -pe '$_.chop!.reverse!; $_ += "\n"' foo
Let Ruby chop for you:
ruby -lpe '$_.reverse!' foo
In-place file editing:
$ cat foo
qwe
123
bar
$ ruby -i -lpe '$_.reverse!' foo
$ cat foo
ewq
321
rab
Just that:
cat example.rb | ruby -ne 'puts " " * 4 + $_'
Or even that:
ruby -ne 'puts " " * 4 + $_' example.rb
ruby -ne 'puts "#$. #$_"' foo.rb bar.rb
ruby -ne '$. = 1 if $<.pos - $_.size == 0; puts "#$. #$_"' foo.rb bar.rb
ruby -ane 'w = (w || 0) + $F.size; END { p w }' exmpl.txt
ruby -ne 'puts $_ if /^[^\n]/../^$/'
Input:
$ cat comments.txt
some code
/*
comment one
comment two
*/
more code
/* one-line comment */
a bit more code
/*
comment three
*/
even more code
Ruby in action:
$ ruby -ne 'puts $_ if ($_ =~ /^\/\*/)..($_ =~ /\*\/$/)' comments.txt
/*
comment one
comment two
*/
/* one-line comment */
/*
comment three
*/
Alternative:
ruby -pe 'next unless ($_ =~ /^\/\*/)..($_ =~ /\*\/$/)' comments.txt
Homebrew flip-flop (from "The Ruby programming language" by David Flanagan, Yukihiro Matsumoto):
# flip-flop.rb
$state = false
def flipflop(s)
unless $state
result = (s =~ /^\/\*$/)
if result
$state = !(s =~ /^\*\/$/)
end
result
else
$state = !(s =~ /^\*\/$/)
true
end
end
ARGF.each_line do |l|
puts l if flipflop(l)
end
Try it:
ruby flip-flop.rb comments.txt
ruby -lpe '$_.gsub! /(\s+)$/, "\e[41m\\1\e[0m"' example.rb
ruby -lpe '$_.rstrip!' example.rb
Easy way:
ruby -ne 'puts "#{$_}\e[31m#{$_.chop!.slice!(60..-1)}\e[0m"' example.rb
Uglier, but configurable:
ruby -e 'w = $*.shift; $<.each { |l| puts "#{l}\e[31m#{l.chop!.slice!(w.to_i..-1)}\e[0m" }' 50 example.rb
Straightforward one:
ruby -e 'loop { puts eval(gets) }'
Leverage -n option:
ruby -ne 'puts eval($_)'
Prompt (but not in the 1st line):
ruby -ne 'print "#{eval($_).inspect}\n>> "'
Fix it:
ruby -ne 'BEGIN{print">> "}; print "#{eval($_).inspect}\n>> "'
REPL should be polite:
ruby -ne 'BEGIN{print">> "}; print "#{eval($_).inspect}\n>> "; END{puts "Bye"}'
Handle RuntimeError (e.g., NameError), but not SyntaxError:
ruby -ne 'BEGIN{print">> "}; print "#{(eval($_) rescue $!).inspect}\n>> "; END{puts "Bye"}'
Rescue everything - save the world:
ruby -ne 'BEGIN{print">> "}; print "#{(begin; eval($_); rescue Exception; $!; end).inspect}\n>> "; END{puts "Bye"}'
Copyright in the beginning of every file:
# copyright.rb
copyright = DATA.read
ARGF.each_line do |l|
puts copyright if $. == 1
puts l
end
__END__
####################################
# Copyright (C) 2012 Kirill Lashuk #
####################################
Use it:
ruby copyright.rb foo.py > foo2.py
In place edit:
ruby -i copyright.rb foo.py
Alternative:
# copyright2.rb
BEGIN { copyright = DATA.read }
puts copyright if $. == 1
__END__
####################################
# Copyright (C) 2012 Kirill Lashuk #
####################################
Use it (with backuping original file):
$ cat bar.py
print 'hello'
$ ruby -p -i.bak copyright2.rb bar.py
$ cat bar.py
####################################
# Copyright (C) 2012 Kirill Lashuk #
####################################
print 'hello'
$ cat bar.py.bak
print 'hello'