According to the CLI ruby โโlink,
-n assume 'while gets(); ... end' loop around your script -e 'command' one line of script. Several -e allowed. Omit [programfile]
So, just copy the code snippet into a ruby โโfile enclosed in a gets () loop
foobar.rb
while gets() @found=true if $_ =~ /foo/ next unless @found puts $_ exit if $_ =~ /bar/ end
And run the file using
ruby foobar.rb < file.txt
You can also replace the IO redirection by reading the file programmatically
file = File.new("file.txt", "r") while (line = file.gets) @found=true if line =~ /foo/ next unless @found puts line exit if line =~ /bar/ end
source share