grep pattern next two lines and discard the pattern with one line

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












-1















I have a file something like this:



dn: danan



cn: danian



cn: danian1




dn: danian2



cn: danian2




dn: danian3



cn: danian3



cn: danian4



all I want to do is grep the pattern "dn:" and print just the next 2 lines, but when I run grep -A 2 "dn:" the result is everything in the file, I just want to print the next 2 lines not the next 1 line










share|improve this question
























  • What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

    – finswimmer
    Jan 22 at 20:23











  • I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

    – danian
    Jan 22 at 20:57












  • root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

    – danian
    Jan 22 at 21:00












  • Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

    – Jeff Schaller
    Jan 22 at 21:08






  • 1





    I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

    – wef
    Jan 22 at 22:10















-1















I have a file something like this:



dn: danan



cn: danian



cn: danian1




dn: danian2



cn: danian2




dn: danian3



cn: danian3



cn: danian4



all I want to do is grep the pattern "dn:" and print just the next 2 lines, but when I run grep -A 2 "dn:" the result is everything in the file, I just want to print the next 2 lines not the next 1 line










share|improve this question
























  • What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

    – finswimmer
    Jan 22 at 20:23











  • I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

    – danian
    Jan 22 at 20:57












  • root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

    – danian
    Jan 22 at 21:00












  • Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

    – Jeff Schaller
    Jan 22 at 21:08






  • 1





    I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

    – wef
    Jan 22 at 22:10













-1












-1








-1


1






I have a file something like this:



dn: danan



cn: danian



cn: danian1




dn: danian2



cn: danian2




dn: danian3



cn: danian3



cn: danian4



all I want to do is grep the pattern "dn:" and print just the next 2 lines, but when I run grep -A 2 "dn:" the result is everything in the file, I just want to print the next 2 lines not the next 1 line










share|improve this question
















I have a file something like this:



dn: danan



cn: danian



cn: danian1




dn: danian2



cn: danian2




dn: danian3



cn: danian3



cn: danian4



all I want to do is grep the pattern "dn:" and print just the next 2 lines, but when I run grep -A 2 "dn:" the result is everything in the file, I just want to print the next 2 lines not the next 1 line







shell-script text-processing grep regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 10:50









Jeff Schaller

41.2k1056131




41.2k1056131










asked Jan 22 at 20:18









daniandanian

11




11












  • What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

    – finswimmer
    Jan 22 at 20:23











  • I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

    – danian
    Jan 22 at 20:57












  • root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

    – danian
    Jan 22 at 21:00












  • Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

    – Jeff Schaller
    Jan 22 at 21:08






  • 1





    I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

    – wef
    Jan 22 at 22:10

















  • What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

    – finswimmer
    Jan 22 at 20:23











  • I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

    – danian
    Jan 22 at 20:57












  • root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

    – danian
    Jan 22 at 21:00












  • Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

    – Jeff Schaller
    Jan 22 at 21:08






  • 1





    I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

    – wef
    Jan 22 at 22:10
















What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

– finswimmer
Jan 22 at 20:23





What do you mean with "he result is everything in the file, I just want to print the next 2 lines not the next 1 line,"? grep -A 2 should print the next 2 lines after each match.

– finswimmer
Jan 22 at 20:23













I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

– danian
Jan 22 at 20:57






I mean , when I run grep -A 2 "dn:" the output is the next 2 lines and the next 1 line i just want to print the 2 next lines

– danian
Jan 22 at 20:57














root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

– danian
Jan 22 at 21:00






root@danian:~# cat dare | grep -A 2 "dn:" dn: cn=danian@gmial.com cn: danian+@hotmail.com cn: danian2B@hotmail.com -- dn: daniandare1@hotmail.com cn: danian1@gf.es --> discard this line becouse is just 1 next line dn: daniandare2@hotmail.com cn: danian2@gf.es --> discard this line becouse is just 1 next line dn: danianlnx@hotmail.com cn: dana@gmail.com cn: fggh@dare.es

– danian
Jan 22 at 21:00














Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

– Jeff Schaller
Jan 22 at 21:08





Do you mean you don't want to see "danian2" from the example, because it has only one line of "cn" text following it, instead of the two lines that the others have?

– Jeff Schaller
Jan 22 at 21:08




1




1





I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

– wef
Jan 22 at 22:10





I think you need to put the desired output into the question where it can be legibly formatted instead of as a comment. Also, please read up on how to format a section as code in the question.

– wef
Jan 22 at 22:10










2 Answers
2






active

oldest

votes


















1














This might work:



$ awk -v RS="dn: " -v FS="n" -v ORS="" 'NF>3 print "dn:",$0' input.txt


We define dn: as a record separator. The fields in one record are delimited by a new line character n.



In case you have two lines following a line starting with dn:, before a new record starts with dn:, you will have 3 times a "n" leading to 4 fields in the record. This is why we check if there are more than 3 fields in the record (NF>3). If that is the case, we print out the whole record, but need to prepend the dn:.



Note that NF>3 will find all records with more than two following lines in one record. If you want only those with exact two change it to NF==3.



If the blocks are delimited by a blank line (as it seems in one of your later comments) use this instead:



$ awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' input.txt





share|improve this answer




















  • 1





    Instead of piping to grep, you could pass in -v ORS="".

    – Niko Gambt
    Jan 23 at 5:43












  • Very good. I've edited my answer.

    – finswimmer
    Jan 23 at 5:45


















-1














I run the command above and works perfectly,



awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' 


the blocks delimited are blanck, it works perfectly.






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',
    autoActivateHeartbeat: false,
    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496057%2fgrep-pattern-next-two-lines-and-discard-the-pattern-with-one-line%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This might work:



    $ awk -v RS="dn: " -v FS="n" -v ORS="" 'NF>3 print "dn:",$0' input.txt


    We define dn: as a record separator. The fields in one record are delimited by a new line character n.



    In case you have two lines following a line starting with dn:, before a new record starts with dn:, you will have 3 times a "n" leading to 4 fields in the record. This is why we check if there are more than 3 fields in the record (NF>3). If that is the case, we print out the whole record, but need to prepend the dn:.



    Note that NF>3 will find all records with more than two following lines in one record. If you want only those with exact two change it to NF==3.



    If the blocks are delimited by a blank line (as it seems in one of your later comments) use this instead:



    $ awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' input.txt





    share|improve this answer




















    • 1





      Instead of piping to grep, you could pass in -v ORS="".

      – Niko Gambt
      Jan 23 at 5:43












    • Very good. I've edited my answer.

      – finswimmer
      Jan 23 at 5:45















    1














    This might work:



    $ awk -v RS="dn: " -v FS="n" -v ORS="" 'NF>3 print "dn:",$0' input.txt


    We define dn: as a record separator. The fields in one record are delimited by a new line character n.



    In case you have two lines following a line starting with dn:, before a new record starts with dn:, you will have 3 times a "n" leading to 4 fields in the record. This is why we check if there are more than 3 fields in the record (NF>3). If that is the case, we print out the whole record, but need to prepend the dn:.



    Note that NF>3 will find all records with more than two following lines in one record. If you want only those with exact two change it to NF==3.



    If the blocks are delimited by a blank line (as it seems in one of your later comments) use this instead:



    $ awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' input.txt





    share|improve this answer




















    • 1





      Instead of piping to grep, you could pass in -v ORS="".

      – Niko Gambt
      Jan 23 at 5:43












    • Very good. I've edited my answer.

      – finswimmer
      Jan 23 at 5:45













    1












    1








    1







    This might work:



    $ awk -v RS="dn: " -v FS="n" -v ORS="" 'NF>3 print "dn:",$0' input.txt


    We define dn: as a record separator. The fields in one record are delimited by a new line character n.



    In case you have two lines following a line starting with dn:, before a new record starts with dn:, you will have 3 times a "n" leading to 4 fields in the record. This is why we check if there are more than 3 fields in the record (NF>3). If that is the case, we print out the whole record, but need to prepend the dn:.



    Note that NF>3 will find all records with more than two following lines in one record. If you want only those with exact two change it to NF==3.



    If the blocks are delimited by a blank line (as it seems in one of your later comments) use this instead:



    $ awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' input.txt





    share|improve this answer















    This might work:



    $ awk -v RS="dn: " -v FS="n" -v ORS="" 'NF>3 print "dn:",$0' input.txt


    We define dn: as a record separator. The fields in one record are delimited by a new line character n.



    In case you have two lines following a line starting with dn:, before a new record starts with dn:, you will have 3 times a "n" leading to 4 fields in the record. This is why we check if there are more than 3 fields in the record (NF>3). If that is the case, we print out the whole record, but need to prepend the dn:.



    Note that NF>3 will find all records with more than two following lines in one record. If you want only those with exact two change it to NF==3.



    If the blocks are delimited by a blank line (as it seems in one of your later comments) use this instead:



    $ awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' input.txt






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 23 at 12:34

























    answered Jan 23 at 5:17









    finswimmerfinswimmer

    52416




    52416







    • 1





      Instead of piping to grep, you could pass in -v ORS="".

      – Niko Gambt
      Jan 23 at 5:43












    • Very good. I've edited my answer.

      – finswimmer
      Jan 23 at 5:45












    • 1





      Instead of piping to grep, you could pass in -v ORS="".

      – Niko Gambt
      Jan 23 at 5:43












    • Very good. I've edited my answer.

      – finswimmer
      Jan 23 at 5:45







    1




    1





    Instead of piping to grep, you could pass in -v ORS="".

    – Niko Gambt
    Jan 23 at 5:43






    Instead of piping to grep, you could pass in -v ORS="".

    – Niko Gambt
    Jan 23 at 5:43














    Very good. I've edited my answer.

    – finswimmer
    Jan 23 at 5:45





    Very good. I've edited my answer.

    – finswimmer
    Jan 23 at 5:45













    -1














    I run the command above and works perfectly,



    awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' 


    the blocks delimited are blanck, it works perfectly.






    share|improve this answer





























      -1














      I run the command above and works perfectly,



      awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' 


      the blocks delimited are blanck, it works perfectly.






      share|improve this answer



























        -1












        -1








        -1







        I run the command above and works perfectly,



        awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' 


        the blocks delimited are blanck, it works perfectly.






        share|improve this answer















        I run the command above and works perfectly,



        awk -v RS="nn" -v FS="n" -v ORS="nn" 'NF>2 print $0' 


        the blocks delimited are blanck, it works perfectly.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 23 at 14:58









        Archemar

        20.1k93772




        20.1k93772










        answered Jan 23 at 12:01









        daniandanian

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496057%2fgrep-pattern-next-two-lines-and-discard-the-pattern-with-one-line%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay