Reputation: 1895
When I run this from the terminal:
$ grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb | cut -d: -f1 | uniq
It returns a single result:
spec/features/admins/change_payment_method_spec.rb
When I run this command (notice InfoPage is now TeamPage:
$ grep -rnw 'PageObjects::CompanySettings::TeamPage' spec/**/*_spec.rb | cut -d: -f1 | uniq
I get one result:
spec/features/team_page_spec.rb
Now, when I go into irb
, and use the backticks to call a shell command, I get output for the second command (TeamPage), but no output for the first command (InfoPage).
2.1.6 :001 > `grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb | cut -d: -f1 | uniq`
""
2.1.6 :002 > # No result ^^
2.1.6 :003 > `grep -rnw 'PageObjects::CompanySettings::TeamPage' spec/**/*_spec.rb | cut -d: -f1 | uniq`
"spec/features/team_page_spec.rb\n"
2.1.6 :004 > # One result, as expected!
Can anyone help me figure out why this is?
Upvotes: 0
Views: 54
Reputation: 369458
I notice that in your first example, the file is nested several directories deep, and that you are using the non-standard **
wildcard.
My best guess is that your interactive shell supports that non-standard wildcard, but your batch shell does not.
One possible solution would be to use find
instead of relying on shell wildcards. Another possibility would be to use Ruby, since you are already using Ruby anyway. (Note: grep
is extremely optimized, so if you have a huge number of very large files, Ruby might be a bit slower.)
# Ruby's Dir class does support **
Dir['spec/**/*_spec.rb'].select {|filename|
File.open(filename) {|file|
file.each_line.any? {|line|
line.inlcude?('PageObjects::CompanySettings::InfoPage')
}
}
}
[UNTESTED]
Upvotes: 1