Invert return value of grep command?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    Invert return value of grep command?

    If grep is successful it returns 0 - how can I change it so that it returns a value other than 0?

    thank you
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    use the -v option of grep

    Comment

    • gary turner
      New Member
      • Mar 2012
      • 1

      #3
      I've been very frustrated at repeatedly finding this answer to this question, which is in fact completely unhelpful.
      Using -v will return 0 for any file that contains lines that don't contain the search pattern, which is generally true, and therefore generally useless!
      The 'real' answer IMHO is that you need to do this using your OS/shell to reverse the return code. On Win32 >Win2k you can use this:
      cmd /c "grep pattern $< & if not ERRORLEVEL 1 exit 1"
      I imagine that on unix shells this should be simpler and less cryptic, but will leave that to someone more expert than myself to comment on should they care.

      Comment

      • no2pencil
        New Member
        • Mar 2012
        • 4

        #4
        My apologies if I don't understand the question, but in regards to grep returning zero upon sucess :

        hpweb# cat xd_receiver.htm | grep facebook
        hpweb# echo $?
        0
        hpweb# cat xd_receiver.htm | grep FACEBOOK
        hpweb# echo $?
        1

        Comment

        Working...