Grep Command Issue

Discussion in 'Programming' started by parallelsruler, Dec 31, 2010.

  1. #1
    Hi All,

    I am new to unix and is trying with a few things. I am encountering the below error when executing a command like grep -B 2 'string' sample.txt

    grep: illegal option -- B
    usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
    [-f pattern_file...] [file...]
    usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
    -f pattern_file... [file...]
    usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]


    Can anybody tell me the reason for this error.

    Thanks in advance and wishing you all a very happy new year.
     
    parallelsruler, Dec 31, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    use lowercase b

    grep -b 2 'string' sample.txt
     
    shofstetter, Dec 31, 2010 IP
  3. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #3
    grep -B 2 ... should work. Worked on my Mac [terminal] using darwin. did you check the man pages on your system?

    Q...
     
    QiSoftware, Jan 1, 2011 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Unlike Mac/PC, Unix is always case sensitive. This is also true for directory name, database names, tables names, etc..
     
    ThePHPMaster, Jan 1, 2011 IP
  5. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #5
    Partial man page for grep:

    
           -B NUM, --before-context=NUM
                  Print  NUM  lines  of  leading  context  before  matching lines.
                  Places  a  line  containing  --  between  contiguous  groups  of
                  matches.
    
           -C NUM, --context=NUM
                  Print  NUM lines of output context.  Places a line containing --
                  between contiguous groups of matches.
    
           -b, --byte-offset
                  Print the byte offset within the input file before each line  of
                  output.
    :
    Code (markup):
    I took case sensitivity into consideration.


    Q...
     
    Last edited: Jan 1, 2011
    QiSoftware, Jan 1, 2011 IP
  6. parallelsruler

    parallelsruler Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    grep -B is not working for me in only one server. On other servers it is working fine. So i doubt if we have to install anything else for this option to work. Anybody has any idea.
     
    parallelsruler, Jan 1, 2011 IP
  7. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #7
    what is the output of "grep -V"
     
    shofstetter, Jan 1, 2011 IP
  8. Alfonso_Montenegro

    Alfonso_Montenegro Peon

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8


    Hmmm.... I will look into this...
     
    Alfonso_Montenegro, Jan 1, 2011 IP