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
Simple newbie grep question doubleminus UNIX for Dummies Questions & Answers 5 04-06-2008 03:05 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 09:03 AM
Simple grep - Not sure it makes sense! GNMIKE UNIX for Dummies Questions & Answers 5 10-21-2005 11:51 PM
Simple grep question, but I'm out of practice citygov Shell Programming and Scripting 0 08-02-2005 07:31 AM
Simple grep questions nitin UNIX for Dummies Questions & Answers 2 10-14-2001 09:52 PM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-05-2003
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Question simple grep question

I have seen this used several times but not really sure of what it actually does. I am confused with the second grep as the argument to the first.

some commands | grep -v grep | some other commands

Can anyone provide an explanation?

Thanks,
Forum Sponsor
  #2 (permalink)  
Old 01-05-2003
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
what that command does is just filter out any text containing 'grep'. for instance if you want to look at a log file,
$> cat 'somelogfile' | grep -v grep | less
thats a pretty simple example, broken down, this is what it does, the command cat displays the contents of a file to the standard output. piping the cat 'somelogfile' to grep with the -v means to exclude anything entries thant contain the text 'grep' then piped to your pager, less, in this example. though cat is not really needed in that example, you could do it with just :
grep -v grep 'somelogfile' | less
thats the only thingi could think of right now. so really that second grep isnt a command just a string to be searched for, and in this case, filtered out.
  #3 (permalink)  
Old 01-05-2003
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
To help with another example, it is usually used with the ps command.

For example, if you run 'ps -ef | grep sh' the output will contain all the running sh shells plus the 'grep sh' command itself. Now if you don't want to see the 'grep sh' in your ps -ef listing, you would run 'ps -ef |grep sh | grep -v grep' or 'ps -ef |grep -v grep| grep sh'. Both will have the same output, I am not sure if one will be faster then the other but if I had to guess I would say it is probably the first one because the 'grep sh' should shorten the list causing grep -v to have less to work with.
  #4 (permalink)  
Old 01-10-2003
danhodges99
Guest
 

Posts: n/a
grep -v xxx simply means 'don't return xxx'. Eg if you do a
ps -ef|grep -v grep, it will return all running processes except your grep.
  #5 (permalink)  
Old 01-27-2003
Registered User
 

Join Date: Sep 2002
Posts: 14
ps -ef |grep vold | wc -l
2


ps -ef |grep vold | grep -v grep| wc -l
1
  #6 (permalink)  
Old 01-27-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,354
Try:
ps -ef | grep "[v]old" | wc -l

"[v]old" is a regular expression that will match "vold" but will not match itself. This eliminates the need for that "grep -v grep" and thus will save a process.
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:29 AM.


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