UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 16th, 2005, 12:21 PM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
Using grep

Hi everyone,

I've read the grep and egrep man pages, and have been unable to figure out the syntax for doing something seemingly simple. I want to be able to grep the piped output of another command (i.e. 'cat MyFile | grep etc, etc) for either:

'foo' or 'bar'

OR

'foo' and 'bar'


The grep man page says the '|' pipe character will function as an 'or' operator, but I can't get the syntax right. And I have no clue how to do the 'and'.

Any ideas?

Reply With Quote
  #2  
Old March 16th, 2005, 01:47 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 10 m 46 sec
Reputation Power: 9
note: grep, sed, awk and 1000 other *nix progs are all able to open a file
so
cat xx ¦ grep zz
is just stupid, try instead
grep zz xx
sure
cat a b c d e ¦ grep zz
could be reasonable and is not the same as
grep zz a b c d e
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #3  
Old March 16th, 2005, 01:50 PM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
I'm actually not using cat, but rather another command. It was just an example of the redirect.

Were you just pointing out the "stupidity" of my example, or do you have any useful suggestions?

Quote:
Originally Posted by guggach
note: grep, sed, awk and 1000 other *nix progs are all able to open a file
so
cat xx ¦ grep zz
is just stupid, try instead
grep zz xx
sure
cat a b c d e ¦ grep zz
could be reasonable and is not the same as
grep zz a b c d e

Reply With Quote
  #4  
Old March 16th, 2005, 02:00 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 10 m 46 sec
Reputation Power: 9
Quote:
I'm actually not using cat, but rather another command. It was just an example of the redirect.

bad example

maybe you want
egrep "aaa|bbb|ccc" filea fileb filec
to get all: aaa bbb and ccc from file[abc]

Reply With Quote
  #5  
Old March 16th, 2005, 02:29 PM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
I repeat - not using cat. There are no files involved (and I'd rather not output to file then run grep on the file) - forget all about cat and files. OK? OK. I'm piping the output of one command into grep, like so:

sudo MyAdminCommand -opt1 -arg1 | grep <strings here>

So, think of my original post in that context. I want to be able to run my command, pipe the output into grep, then grep 2 strings, with either 'or' or 'and' [So 'foo' and 'bar' *OR* 'foo' or 'bar'].

Make sense?

Reply With Quote
  #6  
Old March 17th, 2005, 07:11 PM
munkfish's Avatar
munkfish munkfish is offline
funky munky
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2001
Location: UK
Posts: 1,446 munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 33 m
Reputation Power: 10
Code:
# string with foo or bar in it:
sudo MyAdminCommand -opt1 -arg1 | grep -E "foo|bar"

# string with foo and bar in it:
sudo MyAdminCommand -opt1 -arg1 | grep "foo" | grep "bar"


lots of ways of doing it really, that's just two.


PS: the syntax will depend on your version of grep, see the grep man page for your system for more details.
Comments on this post
html-bonehead disagrees!

Reply With Quote
  #7  
Old March 17th, 2005, 07:41 PM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
Thanks so much - both do exactly what I wanted.

Reply With Quote
  #8  
Old March 18th, 2005, 02:55 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 10 m 46 sec
Reputation Power: 9
Quote:
Originally Posted by html-bonehead
Thanks so much - both do exactly what I wanted.


NO, i dont believe it.

my grep ignores the opt -E (i use egrep for this)
imo
sudo MyAdminCommand -opt1 -arg1 | grep -E "foo|bar"
should grep lines containing foo OR bar
and, nuance
sudo MyAdminCommand -opt1 -arg1 | grep "foo" | grep "bar"
(quotes are here useless) line containing foo AND bar
Comments on this post
html-bonehead disagrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Using grep


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway