Click to See Complete Forum and Search --> : how to make grep work the opposite way


micro
06-18-2004, 07:57 PM
How can I instruct grep to ignore the strings according to a pattern instead of matching them?

If for example I did:
ps -ef | grep tty the result would be obvious.
But how can I instruct it to return the lines that do not have the "tty" substring?

Regular expressions maybe?
How about an example?
Thanks.

maccorin
06-18-2004, 07:57 PM
look for the -v option in the manpage

micro
06-19-2004, 07:09 PM
Thanks.
How about doing this with a regular expression?

maccorin
06-19-2004, 07:28 PM
doing what w/ regexes? if you wanna just view the non tty lines grep -v is your best route. if you wanna do something w/ each line try:

[code]
ps -ef | perl -pe 'if(!/tty/) { do_something(); }'

ecks
06-20-2004, 12:01 PM
It should be the ^ sign but that doesn't work. Goto http://tldp.org/LDP/abs/html/x12544.html. I think bash is horrible at regular expressions though.

maccorin
06-20-2004, 12:08 PM
the ^ sign means "beginning of line" if it's the first thing in a regex like


/^match_something_else_to/


or it can be used in the beginning of a character class to say "anything _but_ these chars" like


# don't match the line if it starts w/ a $ or a /
:\s*[^\$/].*:


he's looking to match anything that doesn't contain the a word, the easiest way to do that is just check if it _does_ contain the word instead and if the match fails then do whatever you need to do [ hence the if(!/regex/) ]

bwkaz
06-20-2004, 02:19 PM
Or use grep's -v option, which accomplishes pretty much the same thing... ;)