The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
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 03:12 AM
grep command debasis.mishra Shell Programming and Scripting 1 03-27-2006 10:53 PM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2003
Registered User
 

Join Date: Nov 2003
Posts: 3
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 (permalink)  
Old 11-14-2003
jsilva's Avatar
Registered User
 

Join Date: Apr 2003
Posts: 169
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 (permalink)  
Old 11-15-2003
kduffin's Avatar
UN1X
 

Join Date: Nov 2003
Location: Virginia
Posts: 441
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 06:29 PM.
  #4 (permalink)  
Old 11-17-2003
jsilva's Avatar
Registered User
 

Join Date: Apr 2003
Posts: 169
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 (permalink)  
Old 11-17-2003
kduffin's Avatar
UN1X
 

Join Date: Nov 2003
Location: Virginia
Posts: 441
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 06:29 PM.
  #6 (permalink)  
Old 11-17-2003
kduffin's Avatar
UN1X
 

Join Date: Nov 2003
Location: Virginia
Posts: 441
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 06:30 PM.
  #7 (permalink)  
Old 11-17-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,354
Quote:
Originally posted by kduffin
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
keith@duffin.org
http://www.duffin.org
truss is terrible tool for performance analysis. Try this as a non-root user:
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 UNIX.COM
Closed Thread

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0