How can you grep for three fields | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How can you grep for three fields

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-29-2008
gugs gugs is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 24 October 2008, 8:26 AM EDT
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
How can you grep for three fields

I have data, from which I want to grep for two fields. Only pull out the data if both the fields exist.

I have used: egrep --text "field1|field2" file > temp. This seems to be doing an OR. What I am after is an AND.

Sponsored Links
    #2  
Old 07-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 18 April 2014, 8:44 AM EDT
Location: Out running a Marathon.
Posts: 2,308
Thanks: 66
Thanked 150 Times in 140 Posts
Hammer & Screwdriver The logic you are using is "or"

When you have the "|" in your command, that is telling the system to do OR logic.

The typical AND logic for grep is to simply complete two greps. Something like the following:


Code:
cat myfile | grep "text1" | grep "text2" >newfile

Sponsored Links
    #3  
Old 07-29-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
You can also use awk :
Code:
awk '/text1/ && /text2/' file > newfile

Jean-Pierre.
    #4  
Old 07-29-2008
DeCoTwc DeCoTwc is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 13 March 2014, 7:24 PM EDT
Location: NYC
Posts: 162
Thanks: 7
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by joeyg View Post
When you have the "|" in your command, that is telling the system to do OR logic.

The typical AND logic for grep is to simply complete two greps. Something like the following:


Code:
cat myfile | grep "text1" | grep "text2" >newfile

Be careful or the "don't cat to grep" mafia is going to get you...
Sponsored Links
    #5  
Old 07-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 18 April 2014, 8:44 AM EDT
Location: Out running a Marathon.
Posts: 2,308
Thanks: 66
Thanked 150 Times in 140 Posts
Hammer & Screwdriver re: don't cat to grep

Quote:
Originally Posted by joeyg

cat myfile | grep "text1" | grep "text2" >newfile

It can just be kind of difficult for some to understand the following:


Code:
grep "text1" <myfile | grep "text2" >newfile

So, while some might 'herd' that 'cat' - I was simply trying to show a sample of multiple greps.
Sponsored Links
    #6  
Old 07-29-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 March 2014, 6:30 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Why run grep twice UUOG

Code:
grep "text1.*text2" myfile > newfile


Last edited by danmero; 07-29-2008 at 06:05 PM..
Sponsored Links
    #7  
Old 07-30-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
Quote:
Originally Posted by danmero View Post
Why run grep twice UUOG

Code:
grep "text1.*text2" myfile > newfile

What about lines containing text2 before text1 ?

Jean-Pierre.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to grep rows by multiple fields yifangt Shell Programming and Scripting 6 05-02-2012 09:01 AM
ignore fields to check in grep ashwin3086 Shell Programming and Scripting 4 07-07-2010 08:53 AM
grep on Hex fields rthiele UNIX for Dummies Questions & Answers 3 06-29-2010 03:55 PM
How to grep more than 2 fields? *Jess* Shell Programming and Scripting 8 10-17-2008 04:28 AM
Grep Line with Matching Fields hemangjani UNIX for Advanced & Expert Users 13 08-10-2007 11:46 AM



All times are GMT -4. The time now is 02:14 PM.