0

I have a simple script that scrapes a webpage and puts the lines of content to the screen, then I simply pipe it to grep to output what I want then pipe that to less.

myscript.rb scrape-term | grep argument | less

I changed the script to use the following, instead of having the extra arguments on the command-line;

%x[ #{my-text-output} | grep argument | less ]

but now I get the error;

sh: 0: command not found

I've tried the other variants found here but nothing works!

Community
  • 1
  • 1
raphael_turtle
  • 7,154
  • 10
  • 55
  • 89

2 Answers2

0

What you need to do is open a handle to grep, not jam the output into there. The hack solution is to dump your output in a Tempfile and read from that through grep but really that's a mess.

Ideally what you do is only emit output that matches your pattern by filtering:

while (...)
  # ...

  if (output.match(/#{argument}/))
    puts argument
  end
end

Then this can be channelled through to less if required.

Remember there's also a grep method inside of Ruby for anything in an Array. For example, if you've created an array called output with the lines in it:

output.grep(/#{argument}/).each do |line|
  puts line
end
tadman
  • 208,517
  • 23
  • 234
  • 262
  • I'd prefer to use the system's grep for the flags I use, namely -A and -B – raphael_turtle Sep 04 '12 at 16:45
  • You can do this within Ruby if you buffer all the output in advance. The `each_with_index` method provides line numbers that can be used to extract ranges of entries. If you don't want to bother with this, use a `Tempfile` and fire that through your `grep | less` pipe. – tadman Sep 04 '12 at 17:50
-1

Since you're going to run it in a shell anyway, because of a less command, then maybe try this:

%x[ echo #{my-text-output} | grep argument | less ]

You can also try to do echo -e which will display \n as newline etc.

(Ideally, your text should have a \n char at the end of each line already.)

Edit:

Forgot about "" for variable. No more Unknown Command errors, although less doesn't want to work with me. I have this script.rb now:

text = "foo
foobar
foobaraski
bar
barski"

a = %x[ echo "#{text}" | grep foo]

print a

And it gives me:

shell> ruby script.rb 
foo
foobar
foobaraski

Edit 2:

And now it works:

text = "foo
foobar
foobaraski
bar
barski"
system "echo '#{text}' | grep foo | less"

Opens up less with three lines, just as bash command would.

Jakub Naliwajek
  • 707
  • 5
  • 9