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.

2
  • Do you mean the first output as the output for the first file only? Commented Jul 16, 2014 at 12:20
  • COMMAND = "grep ... | grep -v '^grep:' | wc -l" Commented Jul 16, 2014 at 12:39

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

More generally, use &>/dev/null after the command and it's arguments to supress all output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.