grep: Always show context of N lines

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Given a file like



asdasd
123
X
456
X
789
asd
asd
asd
asd


If I run grep: grep -C3 'X' on the above, I get the following results



asdasd
123
X
456
--
456
X
789
asd
asd


It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep, there seems to be no such option. The expected result is as follows:



asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd









share|improve this question









New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
    – JigglyNaga
    Nov 20 at 10:10











  • if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
    – Sundeep
    Nov 20 at 15:03










  • grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
    – kanghj91
    Nov 21 at 1:56














up vote
1
down vote

favorite












Given a file like



asdasd
123
X
456
X
789
asd
asd
asd
asd


If I run grep: grep -C3 'X' on the above, I get the following results



asdasd
123
X
456
--
456
X
789
asd
asd


It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep, there seems to be no such option. The expected result is as follows:



asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd









share|improve this question









New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
    – JigglyNaga
    Nov 20 at 10:10











  • if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
    – Sundeep
    Nov 20 at 15:03










  • grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
    – kanghj91
    Nov 21 at 1:56












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Given a file like



asdasd
123
X
456
X
789
asd
asd
asd
asd


If I run grep: grep -C3 'X' on the above, I get the following results



asdasd
123
X
456
--
456
X
789
asd
asd


It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep, there seems to be no such option. The expected result is as follows:



asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd









share|improve this question









New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Given a file like



asdasd
123
X
456
X
789
asd
asd
asd
asd


If I run grep: grep -C3 'X' on the above, I get the following results



asdasd
123
X
456
--
456
X
789
asd
asd


It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep, there seems to be no such option. The expected result is as follows:



asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd






grep osx






share|improve this question









New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 20 at 9:25





















New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 20 at 8:57









kanghj91

63




63




New contributor




kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






kanghj91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
    – JigglyNaga
    Nov 20 at 10:10











  • if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
    – Sundeep
    Nov 20 at 15:03










  • grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
    – kanghj91
    Nov 21 at 1:56












  • 1




    What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
    – JigglyNaga
    Nov 20 at 10:10











  • if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
    – Sundeep
    Nov 20 at 15:03










  • grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
    – kanghj91
    Nov 21 at 1:56







1




1




What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
– JigglyNaga
Nov 20 at 10:10





What version of OSX are you running, and what does grep --version report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --).
– JigglyNaga
Nov 20 at 10:10













if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03




if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03












grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
– kanghj91
Nov 21 at 1:56




grep --version reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
– kanghj91
Nov 21 at 1:56










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.



perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";

' -- -n=3 -p='X' file





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482913%2fgrep-always-show-context-of-n-lines%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.



    perl -0777 -snE '
    @lines = split /n/;
    for $idx (grep $lines[$_] =~ $p 0...$#lines)
    say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";

    ' -- -n=3 -p='X' file





    share|improve this answer
























      up vote
      0
      down vote













      Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.



      perl -0777 -snE '
      @lines = split /n/;
      for $idx (grep $lines[$_] =~ $p 0...$#lines)
      say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";

      ' -- -n=3 -p='X' file





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.



        perl -0777 -snE '
        @lines = split /n/;
        for $idx (grep $lines[$_] =~ $p 0...$#lines)
        say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";

        ' -- -n=3 -p='X' file





        share|improve this answer












        Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.



        perl -0777 -snE '
        @lines = split /n/;
        for $idx (grep $lines[$_] =~ $p 0...$#lines)
        say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";

        ' -- -n=3 -p='X' file






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 15:00









        glenn jackman

        49.4k469106




        49.4k469106




















            kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.












            kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.











            kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482913%2fgrep-always-show-context-of-n-lines%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?