problem using recursive grep (-r option)
If I use the command:
grep -nH -r "my pattern" *.* I get results back as expected But if the file pattern is like this: grep -nH -r "my pattern" *.log I get no results back (Even though I have a ton of files with this pattern with .log file extension). Am I doing something wrong? |
yes, *.log will be evaluated by the shell, not by grep. So if you have no *.log file in the current dir *.log will remain *.log and grep will try to search pattern in the file named *.log. You can check how the shell evaluates commands by: echo <command>, in your case echo grep -nH -r "pattern" *.log. The option -r has no effect if you gave an invalid file/dir argument (as *.log)
|
*.log is expanded by the shell as a list of files ending with that suffix, so no directories will be recursed (unless they all have the .log suffix).
You could use another grep to filter files by extension: Code:
grep -nH -r "my pattern" * | grep -e '^.*\.log:' Code:
find . -name "*.log" -exec grep -nH "my pattern" {} \; |
re: problem using recursive grep (-r option)
Hmmm I am beginning to prefer the find option now. But
find . -name "*.log" -exec grep -nH "my pattern" {} \; or find . -name "*.log" -exec grep -nH "my pattern" \{\} \; doesn't find anything??? Quote:
|
Well, I used "." as path in my example because on your grep examples you looked for files in the current directory.
Of course, if the directory to search is different than the present one, you should specify it instead of ".". By the way, there's no need to escape the "{}". |
Re: problem using recursive grep (-r option)
For some reason I get:
find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" {} \; find: missing argument to `-exec' Any ideas why? Quote:
|
Must depend on your distro (or $PATH).
Works fine for me on RHEL5 Code:
find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" {} \; |
The only thing I can get to work on Cygwin at the moment is:
find . -name "*.log" | grep -nH -r "my pattern" *.* |
but you not need find to run grep:
grep -nH -r "my pattern" *.* |
Quote:
|
[quote]
The only thing I can get to work on Cygwin [quote] This(!), you should have mentioned it in your first post. Cygwin is a port of *nix tools to MS and may vary from eg native GNU/Linux versions. Try the man pages if it comes with those. |
Quote:
Code:
find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" '{}' \; |
All times are GMT -5. The time now is 07:19 AM. |