The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-06-2008 02:45 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
grep command help ishmael^soyuz Shell Programming and Scripting 4 07-11-2007 09:01 AM
grep command pmsuper UNIX for Dummies Questions & Answers 6 11-22-2006 07:12 AM
grep command debasis.mishra Shell Programming and Scripting 1 03-28-2006 01:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2003
BlueOysterCult BlueOysterCult is offline
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
  #2 (permalink)  
Old 11-14-2003
jsilva's Avatar
jsilva jsilva is offline
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
kduffin kduffin is offline Forum Advisor  
UN1X
  
 

Join Date: Nov 2003
Location: Maryland
Posts: 449
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 10:29 PM..
  #4 (permalink)  
Old 11-17-2003
jsilva's Avatar
jsilva jsilva is offline
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
kduffin kduffin is offline Forum Advisor  
UN1X
  
 

Join Date: Nov 2003
Location: Maryland
Posts: 449
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 10:29 PM..
  #6 (permalink)  
Old 11-17-2003
kduffin's Avatar
kduffin kduffin is offline Forum Advisor  
UN1X
  
 

Join Date: Nov 2003
Location: Maryland
Posts: 449
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 10:30 PM..
  #7 (permalink)  
Old 11-17-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0