|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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:
|
|
#4
|
|||
|
|||
|
Quote:
bad example maybe you want egrep "aaa|bbb|ccc" filea fileb filec to get all: aaa bbb and ccc from file[abc] |
|
#5
|
|||
|
|||
|
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? |
|
#6
|
||||
|
||||
|
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.
__________________
FreeBSD Admin Tips Tricks and Scripts |
|
#7
|
|||
|
|||
|
Thanks so much - both do exactly what I wanted.
|
|
#8
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Using grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|