See lines before or after when matching with grep

When searching with grep, sometime it may be helpful to see the lines around the search string, especially in this is in a piece of code.

To do this you have a couple of options.

-A Will show you the number of lines you specify after the search word.

-B Will show you the number of lines you specify before the search word.

You can combine a combination of the above, or you could you -C with a number, this will then show you the lines above and before, saving you combining the options.

In examples below, you can see I am using 2 lines as an example and searching of the word open. You can see the -A, -B and -C options.

Craig’s MacBook Air:python craigdba$ grep -A 2 open hkc_senior_text.py 
F = open(SCRIPTS+'/output/hkcsenior.txt', 'a')

TDATE = strftime("%d-%m")
--
        RESPONSE = urllib2.urlopen(REQ)
        RESPONSE_URL = RESPONSE.geturl()
        if RESPONSE_URL == URL:
Craig’s MacBook Air:python craigdba$ grep -B 2 open hkc_senior_text.py 
MASTER_DB = os.path.join(DROPBOX, DBFILE)

F = open(SCRIPTS+'/output/hkcsenior.txt', 'a')
--

    try:
        RESPONSE = urllib2.urlopen(REQ)
        
        Craig’s MacBook Air:python craigdba$ grep -C 2 open hkc_senior_text.py 
MASTER_DB = os.path.join(DROPBOX, DBFILE)

F = open(SCRIPTS+'/output/hkcsenior.txt', 'a')

TDATE = strftime("%d-%m")
--
--

    try:
        RESPONSE = urllib2.urlopen(REQ)
        RESPONSE_URL = RESPONSE.geturl()
        if RESPONSE_URL == URL:

Installing and running pylint

Python - Creating a zip file