The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK: Multiple patterns per line Plavixo UNIX for Dummies Questions & Answers 1 05-05-2008 01:31 PM
How to cut multiple patterns from a file? Vijay06 Shell Programming and Scripting 5 11-26-2007 05:38 PM
Can we give multiple patterns to a sed command??? kumarsaravana_s Shell Programming and Scripting 6 03-22-2007 11:19 PM
Grep multiple patterns malaymaru Shell Programming and Scripting 4 09-24-2005 10:20 PM
grep for multiple patterns tselvanin UNIX for Dummies Questions & Answers 1 11-12-2003 03:43 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 5
Grep for Multiple patterns

Hi All,

I have a file. I need to find multiple patterns in a row and need those rows to divert to new file.

I tried using grep -e / -E / -F options as given in man.
But its not working.

==> cat testgrep.txt
william,fernandes,xxxxx
mark,morsov,yyyy
yy=,xx=
yyyy=,xxxx==

==> /usr/xpg4/bin/grep -E 'yy xx' testgrep.txt

It gives not output.
==> /usr/xpg4/bin/grep -F 'yy xx' testgrep.txt


I used pattern file as well
==> cat patterngrep.txt
yy\n
xx\n

/usr/xpg4/bin/grep -e patterngrep.txt testgrep.txt


It is not working.

Please let me if we have anyother method of doing it.

Thanks,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Try

/usr/xpg4/bin/grep -E 'yy|xx' testgrep.txt

(or)

egrep 'yy|xx' testgrep.txt
Reply With Quote
  #3 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 5
Grep for Multiple patterns

Thanks Mahendra.

It worked.
Reply With Quote
  #4 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 5
Mahendra,

I have multiple [patetrns to be checked. There can be additions/reductions in the patterns. I need to use pattern list for that.
Do you have any idea how to use pattern list in a file.

Thanks
Reply With Quote
  #5 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Are you looking for this info ?

man grep :

-f pattern_file
Read one or more patterns from the file named by the
path name pattern_file. Patterns in pattern_file are
terminated by a NEWLINE character. A null pattern can
be specified by an empty line in pattern_file. Unless
the -E or -F option is also specified, each pattern
will be treated as a basic regular expression.


You can save all patterns in a file line by line and then

grep -f pattern_file Actual_file
Reply With Quote
  #6 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 5
I have pattern file

==> cat pattern
def
bc

Datafile
==> cat testgrep.txt
abc,lmn
def,xyz
ghi,h
jkl

I tried this
==> /usr/xpg4/bin/grep -f pattern testgrep.txt
abc,lmn

==> /usr/xpg4/bin/grep -E -f pattern testgrep.txt
abc,lmn

==> /usr/xpg4/bin/grep -F -f pattern testgrep.txt
abc,lmn


I am not getting the second line which contains def.
Reply With Quote
  #7 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 5
Hey Mahendra,

There was some syntax problem.
It worked with paatern file
Datafile
==> cat testgrep.txt
abc,lmn
def,xyz
ghi,h
jkl

==> cat pattern
def
abc


command
==> /usr/xpg4/bin/grep -F -f pattern testgrep.txt
abc,lmn
def,xyz
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:25 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