- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: egrep and patterns
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 10:13 AM
тАО06-05-2009 10:13 AM
egrep and patterns
I know to use egrep like this for the OR
egrep -i '(65535|65536)'
How to do the same thing but with AND
Any Idea
Bests Regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 10:24 AM
тАО06-05-2009 10:24 AM
Re: egrep and patterns
precision: patterns are not in the same line !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 10:25 AM
тАО06-05-2009 10:25 AM
Re: egrep and patterns
a
b
c
d
e
test1
f
g
f
test2
h
h
j
I need to find if this input contains test1 AND test2 !!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 11:18 AM
тАО06-05-2009 11:18 AM
Re: egrep and patterns
http://forums13.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1244229489824+28353475&threadId=1320198
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 01:19 PM
тАО06-05-2009 01:19 PM
Re: egrep and patterns
This is one way (if you insist on 'grep') but requires two passes of your input:
# grep -q test1 myfile && grep -q test2 myfile && echo "ok" || echo "no match"
A faster way would be to use Perl or awk like:
# perl -nle 'm/test2/ and $x=1;m/test1/ and $y=1;last if $x+$y==2;END{print $x+$y==2 ? "ok" : "nomatch"}' myfile
By the way, you have un-evaluated solutions in your earlier thread:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1344958
Feedback there too is appreciated :-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 04:29 PM
тАО06-05-2009 04:29 PM
Re: egrep and patterns
In the event that you want more than a "yes/no" answer to the presence of the AND'ed match (as I provided in the first solutions, above) you could use:
# grep -q test1 myfile && grep -q test2 myfile && grep -E "test1|test2" myfile
...which again "suffers" from multiple passes through the file.
...OR in one pass of the file:
# perl -nle 'm/test1/ and $x=1,push(@a,$_),next;m/test2/ and $y=1,push(@a,$_),next;END{if ($x&&$y) {print for (@a)}}' myfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2009 04:43 PM
тАО06-05-2009 04:43 PM
Re: egrep and patterns
awk '/test1/{s[FNR]=$0}/test2/{f=1;s[FNR]=$0}END{if (f){for(i in s) print s[i]}}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2009 03:27 AM
тАО06-06-2009 03:27 AM
Re: egrep and patterns
You can use multiple passes of grep like JRF said:
grep test1 $(grep -l test2 file-list)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2009 08:07 AM
тАО06-06-2009 08:07 AM
Re: egrep and patterns
what about simple:
grep -e test1 -e test2
emha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2009 04:28 PM
тАО06-06-2009 04:28 PM
Re: egrep and patterns
if file only has test1 and not test2, then it will not work.