delete lines before match except the one above match from stdout

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











up vote
1
down vote

favorite












Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.



stdout:



abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr


desired result:



abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr






share|improve this question





















  • Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
    – Sparhawk
    Jun 25 at 23:40










  • before what match? before a repeated line? before a line that repeats only in the first column?
    – Jeff Schaller
    Jun 26 at 0:18










  • perhaps your example should be a bit more complete (showing the variation in what followed etc)
    – Jeff Schaller
    Jun 26 at 0:19














up vote
1
down vote

favorite












Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.



stdout:



abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr


desired result:



abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr






share|improve this question





















  • Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
    – Sparhawk
    Jun 25 at 23:40










  • before what match? before a repeated line? before a line that repeats only in the first column?
    – Jeff Schaller
    Jun 26 at 0:18










  • perhaps your example should be a bit more complete (showing the variation in what followed etc)
    – Jeff Schaller
    Jun 26 at 0:19












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.



stdout:



abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr


desired result:



abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr






share|improve this question













Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.



stdout:



abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr


desired result:



abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr








share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 0:35
























asked Jun 25 at 23:36









Jer

235




235











  • Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
    – Sparhawk
    Jun 25 at 23:40










  • before what match? before a repeated line? before a line that repeats only in the first column?
    – Jeff Schaller
    Jun 26 at 0:18










  • perhaps your example should be a bit more complete (showing the variation in what followed etc)
    – Jeff Schaller
    Jun 26 at 0:19
















  • Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
    – Sparhawk
    Jun 25 at 23:40










  • before what match? before a repeated line? before a line that repeats only in the first column?
    – Jeff Schaller
    Jun 26 at 0:18










  • perhaps your example should be a bit more complete (showing the variation in what followed etc)
    – Jeff Schaller
    Jun 26 at 0:19















Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
– Sparhawk
Jun 25 at 23:40




Perhaps edit your code to show exactly where uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)
– Sparhawk
Jun 25 at 23:40












before what match? before a repeated line? before a line that repeats only in the first column?
– Jeff Schaller
Jun 26 at 0:18




before what match? before a repeated line? before a line that repeats only in the first column?
– Jeff Schaller
Jun 26 at 0:18












perhaps your example should be a bit more complete (showing the variation in what followed etc)
– Jeff Schaller
Jun 26 at 0:19




perhaps your example should be a bit more complete (showing the variation in what followed etc)
– Jeff Schaller
Jun 26 at 0:19










2 Answers
2






active

oldest

votes

















up vote
1
down vote













You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:



$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr





share|improve this answer





















  • Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
    – Jer
    Jun 26 at 1:36


















up vote
0
down vote













Here is an awk one-liner.



awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt


Explanation




  • /^abc/ prev = $0; prevabc = "true";: If the line starts with abc, don't print it, but instead store the value of the line in the variable prev, and indicated that this line started with abc by storing true in variable prevabc.


  • /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts with xyz, the do the following.


    • if (prevabc == "true") print prev; prevabc = "false" : If the previous line started with abc (because prevabc == "true"), then print the previous line prev and reset prevabc to false.


    • print: Since this line started with xyz, then print this line too.






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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f451880%2fdelete-lines-before-match-except-the-one-above-match-from-stdout%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:



    $ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
    abc jkl
    xyz mno
    xyz pqr
    abc jkl
    xyz mno
    xyz pqr





    share|improve this answer





















    • Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
      – Jer
      Jun 26 at 1:36















    up vote
    1
    down vote













    You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:



    $ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
    abc jkl
    xyz mno
    xyz pqr
    abc jkl
    xyz mno
    xyz pqr





    share|improve this answer





















    • Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
      – Jer
      Jun 26 at 1:36













    up vote
    1
    down vote










    up vote
    1
    down vote









    You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:



    $ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
    abc jkl
    xyz mno
    xyz pqr
    abc jkl
    xyz mno
    xyz pqr





    share|improve this answer













    You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:



    $ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
    abc jkl
    xyz mno
    xyz pqr
    abc jkl
    xyz mno
    xyz pqr






    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jun 26 at 1:00









    steeldriver

    30.9k34877




    30.9k34877











    • Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
      – Jer
      Jun 26 at 1:36

















    • Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
      – Jer
      Jun 26 at 1:36
















    Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
    – Jer
    Jun 26 at 1:36





    Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
    – Jer
    Jun 26 at 1:36













    up vote
    0
    down vote













    Here is an awk one-liner.



    awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt


    Explanation




    • /^abc/ prev = $0; prevabc = "true";: If the line starts with abc, don't print it, but instead store the value of the line in the variable prev, and indicated that this line started with abc by storing true in variable prevabc.


    • /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts with xyz, the do the following.


      • if (prevabc == "true") print prev; prevabc = "false" : If the previous line started with abc (because prevabc == "true"), then print the previous line prev and reset prevabc to false.


      • print: Since this line started with xyz, then print this line too.






    share|improve this answer

























      up vote
      0
      down vote













      Here is an awk one-liner.



      awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt


      Explanation




      • /^abc/ prev = $0; prevabc = "true";: If the line starts with abc, don't print it, but instead store the value of the line in the variable prev, and indicated that this line started with abc by storing true in variable prevabc.


      • /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts with xyz, the do the following.


        • if (prevabc == "true") print prev; prevabc = "false" : If the previous line started with abc (because prevabc == "true"), then print the previous line prev and reset prevabc to false.


        • print: Since this line started with xyz, then print this line too.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Here is an awk one-liner.



        awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt


        Explanation




        • /^abc/ prev = $0; prevabc = "true";: If the line starts with abc, don't print it, but instead store the value of the line in the variable prev, and indicated that this line started with abc by storing true in variable prevabc.


        • /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts with xyz, the do the following.


          • if (prevabc == "true") print prev; prevabc = "false" : If the previous line started with abc (because prevabc == "true"), then print the previous line prev and reset prevabc to false.


          • print: Since this line started with xyz, then print this line too.






        share|improve this answer













        Here is an awk one-liner.



        awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt


        Explanation




        • /^abc/ prev = $0; prevabc = "true";: If the line starts with abc, don't print it, but instead store the value of the line in the variable prev, and indicated that this line started with abc by storing true in variable prevabc.


        • /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts with xyz, the do the following.


          • if (prevabc == "true") print prev; prevabc = "false" : If the previous line started with abc (because prevabc == "true"), then print the previous line prev and reset prevabc to false.


          • print: Since this line started with xyz, then print this line too.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 26 at 0:58









        Sparhawk

        8,25963287




        8,25963287






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451880%2fdelete-lines-before-match-except-the-one-above-match-from-stdout%23new-answer', 'question_page');

            );

            Post as a guest













































































            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