•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Web Developers' Lounge section within the Web Development category of DaniWeb, a massive community of 397,851 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,330 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Web Developers' Lounge advertiser:
Views: 2637 | Replies: 7 | Solved
![]() |
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
grep -r --include=*.php "some_string" some_directory
works in GNU grep 2.5.1
post output of `grep --version` and `grep --help` if that doesn't work... there might be another way.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Jan 2006
Location: Boston, MA
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks for the reply Matt. But that didn't seem to work as expected.
I also noticed that there are two versions of grep available on our server:
/usr/bin/grep
/usr/local/bin/grep
If it type: "which grep"
I get: "/usr/bin/grep"
Here is the output of /usr/bin/grep --help
Here is the output of /usr/bin/grep --version
Here is the output of /usr/local/bin/grep --version
Thanks!
-Dave
I also noticed that there are two versions of grep available on our server:
/usr/bin/grep
/usr/local/bin/grep
If it type: "which grep"
I get: "/usr/bin/grep"
Here is the output of /usr/bin/grep --help
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
Example: grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression
-e, --regexp=PATTERN use PATTERN as a regular expression
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version print version information and exit
--help display this help and exit
-Z, --decompress decompress input before searching (HAVE_LIBZ=1)
--mmap use memory-mapped input if possible
Output control:
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
-H, --with-filename print the filename for each match
-h, --no-filename suppress the prefixing filename on output
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE
TYPE is 'binary', 'text', or 'without-match'.
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories
ACTION is 'read', 'recurse', or 'skip'.
-r, --recursive equivalent to --directories=recurse.
-L, --files-without-match only print FILE names containing no match
-l, --files-with-matches only print FILE names containing matches
-c, --count only print a count of matching lines per FILE
--null print 0 byte after FILE name
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context[=NUM] print NUM (default 2) lines of output context
unless overridden by -A or -B
-NUM same as --context=NUM
-U, --binary do not strip CR characters at EOL (MSDOS)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)
`egrep' means `grep -E'. `fgrep' means `grep -F'.
With no FILE, or when FILE is -, read standard input. If less than
two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,
and 2 if trouble.
Report bugs to <bug-gnu-utils@gnu.org>.Here is the output of /usr/bin/grep --version
grep (GNU grep) 2.4d Copyright (C) 1988, 1992-1998, 1999 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Here is the output of /usr/local/bin/grep --version
grep version 0.9 /usr/local/bin/grepusage: grep [-[AB] num] [-CEFGHLPRSVZabchilnoqsvwx] [-e patttern] [-f file]
Thanks!
-Dave
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
Try this; it relies on the 'find' command.
Which translates in english to.. search (find) in this folder (.) for files matching the filter ( -name ) *.php [ recursiveness should be default ]; when each file is found, execute grep, look for 'some_string', and pass in the found filename ( where the {} placeholder is ). Because that effectively executes grep once per file ( rather than once for many files ), you need the -H option to force grep to turn on the output of the filename; it wouldn't bother otherwise for a single file grep.
Messy eh? No wonder they put this functionality directly into grep for the next version...
EDIT: By the way, use that with the /usr/bin/grep.
find . -name "*.php" -exec grep -H "some_string" {} \; Messy eh? No wonder they put this functionality directly into grep for the next version...
EDIT: By the way, use that with the /usr/bin/grep.
Last edited by MattEvans : Aug 14th, 2007 at 4:54 pm.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
Here's a simpler one in the same vein:
i.e.
note backticks (`) rather than single quotes around the find subcommand..
grep "some_string" `find some_folder -name "*.some_extension"`
grep "cerr" `find . -name "*.cpp"`note backticks (`) rather than single quotes around the find subcommand..
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Jan 2006
Location: Boston, MA
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Many thanks Matt for the excellent help!
The command you posted works well:
But there seems to be a limitation. I found that the command works as expected if used on a directory which does not contain many files. However, when I attempt to use it to search through all files in my site root directory, I get the following error:
My original problem to solve was how to search the contents of all PHP files within a website for a given text string.
Any further advice?
Thank you!
-Dave
The command you posted works well:
grep "some_string" `find some_folder -name "*.some_extension"`
But there seems to be a limitation. I found that the command works as expected if used on a directory which does not contain many files. However, when I attempt to use it to search through all files in my site root directory, I get the following error:
/usr/bin/grep: Argument list too long.
Any further advice?
Thank you!
-Dave
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
lol.. didn't think of that.
did you try the first one I posted? that shouldn't hit the same problem.. because grep is opened with one line of arguement at a time.
otherwise; try this one:
looks similar to the
with the split line set to 10; this one seems to run much, much faster than the other three ways.. You could change -l10 to be higher or lower, if you set it to 1 though you'll need to add the -H flag to grep again.
did you try the first one I posted? that shouldn't hit the same problem.. because grep is opened with one line of arguement at a time.
otherwise; try this one:
find some_dir -name "*.some_ext" | xargs -l10 grep "some_expr"
looks similar to the
find . -name "*.php" -exec grep -H "some_string" {} \; one; but it lets find collect the entire set of files and pipe them to the xargs command, which splits the set into blocks of 10 lines ( controlled using the -l flag ), and calls grep with those blocks of files as the last argument( s )..with the split line set to 10; this one seems to run much, much faster than the other three ways.. You could change -l10 to be higher or lower, if you set it to 1 though you'll need to add the -H flag to grep again.
Last edited by MattEvans : Aug 15th, 2007 at 12:25 pm.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Jan 2006
Location: Boston, MA
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
That solved my problem! (with a small correction)
I found that your command as posted did not work:
I got an error that "-l" was an illegal option. I looked at the man page for xargs and found that it should actually be "-L"
So here's the final command line magic that does exactly what I want:
Many thanks once again to Matt for the excellent suggestions!
Regards,
Dave
I found that your command as posted did not work:
find some_dir -name "*.some_ext" | xargs -l10 grep "some_expr"
So here's the final command line magic that does exactly what I want:
find some_dir -name "*.some_ext" | xargs -L10 grep "some_expr"
Regards,
Dave
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Web Developers' Lounge Marketplace
•
•
•
•
adult advertising blog browser browsers browsing community data design development devices domains firefox google html india internet java legal linux marketing math merger microsoft mobile applications mozilla msn multimedia news php privacy report research search security sex social networking software technology users video w3c web web development wiki wikipedia xml yahoo youtube
- Output a file's type and created date (Java)
- Anyone know what .AL file type is. Please help (Mac Software)
- Specify File type (PHP)
- Convert any file type to pdf with distiller (C#)
- How do I get the default action of a specific file type? (Visual Basic 4 / 5 / 6)
- Windows media player- Does not support file type! (Windows Software)
- Having problems with a file type (Geeks' Lounge)
- Missing Mailto Protocol Reg File (Web Browsers)
Other Threads in the Web Developers' Lounge Forum
- Previous Thread: Old versions of IE
- Next Thread: Apache-PHP problems...


Linear Mode