Posted: . At: 9:33 AM. This was 2 years ago. Post ID: 16410
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to get extra context around search results from grep.


Using grep to find text strings is very useful. This may also be used to view extra text around the matches to get extra context.

Use grep like this.

grep -R -A 10 -B 10 'command arguments' * 2>/dev/null

This returns 10 extra lines of text before and after each matching string.

Below is an example.

┌──(john㉿DESKTOP-PF01IEE)-[/usr/share/doc]
└─$ grep -R -A 10 -B 10 'command arguments' * 2>/dev/null
util-linux/examples/getopt-example.tcsh-# --> `par1'
util-linux/examples/getopt-example.tcsh-# --> `another arg'
util-linux/examples/getopt-example.tcsh-# --> `wow!*\?'
util-linux/examples/getopt-example.tcsh-
util-linux/examples/getopt-example.tcsh-# Note that we had to escape the exclamation mark in the wow-argument. This
util-linux/examples/getopt-example.tcsh-# is _not_ a problem with getopt, but with the tcsh command parsing. If you
util-linux/examples/getopt-example.tcsh-# would give the same line from the bash prompt (ie. call ./parse.tcsh),
util-linux/examples/getopt-example.tcsh-# you could remove the exclamation mark.
util-linux/examples/getopt-example.tcsh-
util-linux/examples/getopt-example.tcsh-# This is a bit tricky. We use a temp variable, to be able to check the
util-linux/examples/getopt-example.tcsh:# return value of getopt (eval nukes it). argv contains the command arguments
util-linux/examples/getopt-example.tcsh-# as a list. The ':q`  copies that list without doing any substitutions:
util-linux/examples/getopt-example.tcsh-# each element of argv becomes a separate argument for getopt. The braces
util-linux/examples/getopt-example.tcsh-# are needed because the result is also a list.
util-linux/examples/getopt-example.tcsh-set temp=(`getopt -s tcsh -o ab:c:: --long a-long,b-long:,c-long:: -- $argv:q`)
util-linux/examples/getopt-example.tcsh-if ($? != 0) then
util-linux/examples/getopt-example.tcsh-  echo "Terminating..." >/dev/stderr
util-linux/examples/getopt-example.tcsh-  exit 1
util-linux/examples/getopt-example.tcsh-endif
util-linux/examples/getopt-example.tcsh-
util-linux/examples/getopt-example.tcsh-# Now we do the eval part. As the result is a list, we need braces. But they
--
zlib1g-dev/examples/enough.c-                            1 << root, 0);
zlib1g-dev/examples/enough.c-            }
zlib1g-dev/examples/enough.c-
zlib1g-dev/examples/enough.c-    /* done */
zlib1g-dev/examples/enough.c-    printf("done: maximum of %d table entries\n", large);
zlib1g-dev/examples/enough.c-}
zlib1g-dev/examples/enough.c-
zlib1g-dev/examples/enough.c-/*
zlib1g-dev/examples/enough.c-   Examine and show the total number of possible Huffman codes for a given
zlib1g-dev/examples/enough.c-   maximum number of symbols, initial root table size, and maximum code length
zlib1g-dev/examples/enough.c:   in bits -- those are the command arguments in that order.  The default
zlib1g-dev/examples/enough.c-   values are 286, 9, and 15 respectively, for the deflate literal/length code.
zlib1g-dev/examples/enough.c-   The possible codes are counted for each number of coded symbols from two to
zlib1g-dev/examples/enough.c-   the maximum.  The counts for each of those and the total number of codes are
zlib1g-dev/examples/enough.c-   shown.  The maximum number of inflate table entires is then calculated
zlib1g-dev/examples/enough.c-   across all possible codes.  Each new maximum number of table entries and the
zlib1g-dev/examples/enough.c-   associated sub-code (starting at root + 1 == 10 bits) is shown.
zlib1g-dev/examples/enough.c-
zlib1g-dev/examples/enough.c-   To count and examine Huffman codes that are not length-limited, provide a
zlib1g-dev/examples/enough.c-   maximum length equal to the number of symbols minus one.
zlib1g-dev/examples/enough.c-

Another example. This provides much more information about each matching string found.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/ubuntu/home/jason/Downloads/NOTREPACKED/nt5src/XPSP1]
└─$ grep -R -A 10 -B 10 ' virus scanner ' * 2>/dev/null
base/ntsetup/win95upg/tools/vcc/vcc.c-    }
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-    //
base/ntsetup/win95upg/tools/vcc/vcc.c-    // Gather information on all the
base/ntsetup/win95upg/tools/vcc/vcc.c-    //
base/ntsetup/win95upg/tools/vcc/vcc.c-    h = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-    if (h != -1) {
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-        //
base/ntsetup/win95upg/tools/vcc/vcc.c:        // Initialize the virus scanner database.
base/ntsetup/win95upg/tools/vcc/vcc.c-        //
base/ntsetup/win95upg/tools/vcc/vcc.c-        fileString = JoinPaths (g_DllDir, TEXT("vscandb.inf"));
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-        if (!InitMigDbEx (fileString)) {
base/ntsetup/win95upg/tools/vcc/vcc.c:            printf ("vcc - Could not initialeze virus scanner database. (GLE: %d)\n", GetLastError());
base/ntsetup/win95upg/tools/vcc/vcc.c-            CloseHandle(h);
base/ntsetup/win95upg/tools/vcc/vcc.c-            return 255;
base/ntsetup/win95upg/tools/vcc/vcc.c-        }
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-        FreePathString (fileString);
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-        SetLastError(ERROR_SUCCESS);
base/ntsetup/win95upg/tools/vcc/vcc.c-
base/ntsetup/win95upg/tools/vcc/vcc.c-        pe.dwSize = sizeof (PROCESSENTRY32);
base/ntsetup/win95upg/tools/vcc/vcc.c-
--
base/ntsetup/win95upg/tools/vswiz/vswiz.c-/*++
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-Copyright (c) 1997 Microsoft Corporation
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-Module Name:
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-    hwwiz.c
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-Abstract:
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c:    Implements an upgrade wizard for gathering virus scanner information.
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-Author:
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-    Marc Whitten (marcw)  16-Oct-1998
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-Revision History:
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c-    <alias> <date> <comments>
base/ntsetup/win95upg/tools/vswiz/vswiz.c-
base/ntsetup/win95upg/tools/vswiz/vswiz.c---*/
--

Yet another example.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/ubuntu/home/jason/Downloads/NOTREPACKED/nt5src/XPSP1]
└─$ grep -R -A 2 -B 2 ' windows setup ' * 2>/dev/null
com/ole32/stg/props/utest/testcase.cxx-    // the machine doesn't support CODEPAGE_GOOD (this is the case by default
com/ole32/stg/props/utest/testcase.cxx-    // on Win95).  To remedy this situation, go to control panel, add/remove
com/ole32/stg/props/utest/testcase.cxx:    // programs, windows setup (tab), check MultiLanguage support, then
com/ole32/stg/props/utest/testcase.cxx-    // click OK.  You'll have to restart the computer after this.
com/ole32/stg/props/utest/testcase.cxx-
--
ds/security/services/w32time/w32time/register.cpp-        
ds/security/services/w32time/w32time/register.cpp-    } else if (ERROR_FILE_NOT_FOUND==dwRetval) {
ds/security/services/w32time/w32time/register.cpp:        // this may be during windows setup and our reg keys are not available yet. Write a special value in the service key
ds/security/services/w32time/w32time/register.cpp-        dwRetval=RegOpenKeyEx(HKEY_LOCAL_MACHINE, wszW32TimeRegKeyRoot, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hkW32Time);
ds/security/services/w32time/w32time/register.cpp-        if (ERROR_SUCCESS!=dwRetval) {
^C

This is a very useful way to get more information about each matching string and better find what you are looking for.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.