how to make grep work the opposite way


Results 1 to 7 of 7

Thread: how to make grep work the opposite way

  1. #1
    Join Date
    Dec 2003
    Location
    Athens Greece
    Posts
    186

    how to make grep work the opposite way

    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.
    http://users.hol.gr/~micro
    Visit my GNU/Linux and Music pages!
    Update your browser's cache:
    Hit refresh inside the updated pages.

  2. #2
    Join Date
    Jan 2004
    Posts
    407
    look for the -v option in the manpage
    DISCLAIMER: sometimes i use a text browser, so I may not see any emoticons directed at me.

    Brandon Niemczyk <bniemczyk@snprogramming.com>
    http://bniemczyk.is-a-geek.com

  3. #3
    Join Date
    Dec 2003
    Location
    Athens Greece
    Posts
    186
    Thanks.
    How about doing this with a regular expression?
    http://users.hol.gr/~micro
    Visit my GNU/Linux and Music pages!
    Update your browser's cache:
    Hit refresh inside the updated pages.

  4. #4
    Join Date
    Jan 2004
    Posts
    407
    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(); }'
    DISCLAIMER: sometimes i use a text browser, so I may not see any emoticons directed at me.

    Brandon Niemczyk <bniemczyk@snprogramming.com>
    http://bniemczyk.is-a-geek.com

  5. #5
    Join Date
    Dec 2003
    Location
    New Jersey
    Posts
    117
    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.

  6. #6
    Join Date
    Jan 2004
    Posts
    407
    the ^ sign means "beginning of line" if it's the first thing in a regex like

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

    Code:
    # 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/) ]
    DISCLAIMER: sometimes i use a text browser, so I may not see any emoticons directed at me.

    Brandon Niemczyk <bniemczyk@snprogramming.com>
    http://bniemczyk.is-a-geek.com

  7. #7
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Or use grep's -v option, which accomplishes pretty much the same thing...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •