0

I'm trying to obtain the output of a bash command. More precisely, I need to store the number of lines that contains a string in a file:

variable_name = AAAAAAA
PATH_TO_SEARCH = .
COMMAND = "grep -R #{variable_name} #{PATH_TO_SEARCH} | wc -l"

To execute the command I tried both methods:

num_lines = %x[ #{COMMAND} ]
num_lines = `#{COMMAND}`

but the problem is: In "num_lines" I have 1) the number of lines that contain the string (OK!) and 2) output from grep like "grep: /home/file_example.txt: No such file or directory" (NO!). I would like to store just the first output.

user3472065
  • 1,259
  • 4
  • 16
  • 32

1 Answers1

1

Looks like you may just need to suppress the error messages.

"You can use the -s or --no-messages flag to suppress errors." found from How can I have grep not print out 'No such file or directory' errors?

Community
  • 1
  • 1
dev
  • 1,477
  • 5
  • 18
  • 43