![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| grep command | christine33990 | UNIX for Dummies Questions & Answers | 11 | 05-05-2008 11:45 PM |
| how to exclude the GREP command from GREP | yamsin789 | UNIX for Advanced & Expert Users | 2 | 10-04-2007 11:59 PM |
| grep command help | ishmael^soyuz | Shell Programming and Scripting | 4 | 07-11-2007 06:01 AM |
| grep command | pmsuper | UNIX for Dummies Questions & Answers | 6 | 11-22-2006 04:12 AM |
| grep command | debasis.mishra | Shell Programming and Scripting | 1 | 03-27-2006 10:53 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
grep command-HELP?!
Hello all
I am having trouble with the grep command. I want to find the word "blue" - ( the word blue in a sentence in the file) in numerous files and directories. I have tried every combo to get there. Can anyone help? I am not going to try to pretend this isn't a homeowrk question.. but believe me I have spent a lot of time trying this to no avail Rob |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Hi,
The "normal" unix grep has no way to do recursive search, you can, for example, use a pipe with find and xargs, like this: find where_to_find | xargs grep what_to_look |
|
#3
|
||||
|
||||
|
Hey Rob,
Using find is probably your best bet. If you are wanting to parse all files in a directory called somedir, you can: $ find /path/to/somedir -type f -exec grep blue {} \; Find is an extremely useful command, you can specify multiple files, access time, modification time etc. etc. The man page is worth a read. The -exec executes the next command and places the argument (in this case the file it found) to the right of the {}. Using exec rather than xargs keeps you to fewer sys calls. Hope this helps. Cheers, Keith Last edited by kduffin; 11-17-2003 at 07:29 PM. |
|
#4
|
||||
|
||||
|
kduffin,
Using -exec will start a grep program for each file it founds, while xargs will be less resources consuming. You can end with hundreds of "grep"s running. |
|
#5
|
||||
|
||||
|
I still don't think it's more expensive. The grep processes won't stack either, so you'll not see hundreds in your proc table. Just as a quick example under a solaris dir with 4 files, using truss to watch the lib calls etc.:
$ truss find . -type f 2>&1 | xargs grep ls 2>&1 | wc -l 378 $ truss find . -type f -exec grep ls {} \; 2>&1 | wc -l 49 The xargs still seems to be the more expensive choice. Cheers, Keith Last edited by kduffin; 11-17-2003 at 07:29 PM. |
|
#6
|
||||
|
||||
|
One thing that the xargs example does do better is that it will show the filenames where as the exec won't. You'd have to have a wrapper that echo's the filename and does a grep for the exec to work effectively.
$ find . -type f | xargs grep ls ./ick1:ls ./ick2:ls ./ick3:ls $ find . -type f -exec grep ls {} \; ls ls ls $ Cheers, Keith Last edited by kduffin; 11-17-2003 at 07:30 PM. |
|
#7
|
||||
|
||||
|
Quote:
cd /usr/bin time find . -print | xargs grep "bogusstring" > /dev/null time find . -exec grep "bogusstring" {} \; > /dev/null Because the commands are running as non-root and we did not redirect stderr you will see a few error messages. Notice the difference in the speed at which are displayed. And then compare the numbers. By using xargs, you prevented hundreds of fork() and hundreds of exec() system calls. Neither system call is cheap. |
||||
| Google The UNIX and Linux Forums |