0

I'm trying to locate txt and html files that contains "some string user input" in their contents.

in the command line the following command works perfect.

grep -ri --include \*.txt --include \*.html string .

but when trying to code it into ruby program as following:

s = ARGV[0]
files = %x(grep -ri --include \*.txt --include \*.html #{s} .) 

and this is how I run my program

$ ruby app.rb string

it doesn't work.

any ideas on how to get it to work that way?

thanks in advance

fizz
  • 3
  • 2

2 Answers2

1

\ escapes * in Ruby, which does not need to be escaped, so the text transmitted to the shell is grep -ri --include *.txt --include *.html filename .. Escape the backslash so that shell gets it intact:

files = %x(grep -ri --include \\*.txt --include \\*.html #{s} .) 
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Great!! that worked. Turned out that I was escaping * just like what you said. Thank you – fizz Sep 08 '14 at 06:34
0
#!/bin/env ruby
# encoding: utf-8
s = ARGV[0]
files = %x|grep -ri --include \*.txt --include \*.html #{s} .| 
puts files

you could use | instead

Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
  • That would work as the sentence too but to solve my issue that I had before i'd have to put it this way: ( --include \\*.txt --include \\*.html) just like @Amadan said. Thank you for the help tho – fizz Sep 08 '14 at 06:37