How to print of the text between the last occurence of a pair of patterns?

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











up vote
0
down vote

favorite












I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:



StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15


I would like the output to be:



11
12
13


How can I do this with sed?










share|improve this question























  • cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
    – Emilio Galarraga
    Sep 26 '17 at 17:26














up vote
0
down vote

favorite












I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:



StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15


I would like the output to be:



11
12
13


How can I do this with sed?










share|improve this question























  • cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
    – Emilio Galarraga
    Sep 26 '17 at 17:26












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:



StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15


I would like the output to be:



11
12
13


How can I do this with sed?










share|improve this question















I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:



StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15


I would like the output to be:



11
12
13


How can I do this with sed?







text-processing awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '17 at 16:17









RomanPerekhrest

22.5k12145




22.5k12145










asked Sep 26 '17 at 16:00









A. B

224




224











  • cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
    – Emilio Galarraga
    Sep 26 '17 at 17:26
















  • cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
    – Emilio Galarraga
    Sep 26 '17 at 17:26















cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
– Emilio Galarraga
Sep 26 '17 at 17:26




cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
– Emilio Galarraga
Sep 26 '17 at 17:26










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










With single awk process:



awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output



output file contents:



11
12
13



Alternative tac + awk solution:



tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output





share|improve this answer






















  • This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
    – A. B
    Sep 26 '17 at 16:12










  • @A.B, it will work even if EndPattern is not the last line. You need to test
    – RomanPerekhrest
    Sep 26 '17 at 16:14











  • Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
    – A. B
    Sep 26 '17 at 16:22










  • @A.B, I can not see the issue. Look here ibb.co/bTqUYk
    – RomanPerekhrest
    Sep 26 '17 at 16:27

















up vote
0
down vote













cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'





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%2f394575%2fhow-to-print-of-the-text-between-the-last-occurence-of-a-pair-of-patterns%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
    0
    down vote



    accepted










    With single awk process:



    awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
    f r=(r=="")? $0: r RS $0 END print r ' file > output



    output file contents:



    11
    12
    13



    Alternative tac + awk solution:



    tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output





    share|improve this answer






















    • This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
      – A. B
      Sep 26 '17 at 16:12










    • @A.B, it will work even if EndPattern is not the last line. You need to test
      – RomanPerekhrest
      Sep 26 '17 at 16:14











    • Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
      – A. B
      Sep 26 '17 at 16:22










    • @A.B, I can not see the issue. Look here ibb.co/bTqUYk
      – RomanPerekhrest
      Sep 26 '17 at 16:27














    up vote
    0
    down vote



    accepted










    With single awk process:



    awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
    f r=(r=="")? $0: r RS $0 END print r ' file > output



    output file contents:



    11
    12
    13



    Alternative tac + awk solution:



    tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output





    share|improve this answer






















    • This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
      – A. B
      Sep 26 '17 at 16:12










    • @A.B, it will work even if EndPattern is not the last line. You need to test
      – RomanPerekhrest
      Sep 26 '17 at 16:14











    • Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
      – A. B
      Sep 26 '17 at 16:22










    • @A.B, I can not see the issue. Look here ibb.co/bTqUYk
      – RomanPerekhrest
      Sep 26 '17 at 16:27












    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    With single awk process:



    awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
    f r=(r=="")? $0: r RS $0 END print r ' file > output



    output file contents:



    11
    12
    13



    Alternative tac + awk solution:



    tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output





    share|improve this answer














    With single awk process:



    awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
    f r=(r=="")? $0: r RS $0 END print r ' file > output



    output file contents:



    11
    12
    13



    Alternative tac + awk solution:



    tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 26 '17 at 16:24

























    answered Sep 26 '17 at 16:05









    RomanPerekhrest

    22.5k12145




    22.5k12145











    • This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
      – A. B
      Sep 26 '17 at 16:12










    • @A.B, it will work even if EndPattern is not the last line. You need to test
      – RomanPerekhrest
      Sep 26 '17 at 16:14











    • Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
      – A. B
      Sep 26 '17 at 16:22










    • @A.B, I can not see the issue. Look here ibb.co/bTqUYk
      – RomanPerekhrest
      Sep 26 '17 at 16:27
















    • This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
      – A. B
      Sep 26 '17 at 16:12










    • @A.B, it will work even if EndPattern is not the last line. You need to test
      – RomanPerekhrest
      Sep 26 '17 at 16:14











    • Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
      – A. B
      Sep 26 '17 at 16:22










    • @A.B, I can not see the issue. Look here ibb.co/bTqUYk
      – RomanPerekhrest
      Sep 26 '17 at 16:27















    This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
    – A. B
    Sep 26 '17 at 16:12




    This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
    – A. B
    Sep 26 '17 at 16:12












    @A.B, it will work even if EndPattern is not the last line. You need to test
    – RomanPerekhrest
    Sep 26 '17 at 16:14





    @A.B, it will work even if EndPattern is not the last line. You need to test
    – RomanPerekhrest
    Sep 26 '17 at 16:14













    Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
    – A. B
    Sep 26 '17 at 16:22




    Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
    – A. B
    Sep 26 '17 at 16:22












    @A.B, I can not see the issue. Look here ibb.co/bTqUYk
    – RomanPerekhrest
    Sep 26 '17 at 16:27




    @A.B, I can not see the issue. Look here ibb.co/bTqUYk
    – RomanPerekhrest
    Sep 26 '17 at 16:27












    up vote
    0
    down vote













    cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'





    share|improve this answer
























      up vote
      0
      down vote













      cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'





        share|improve this answer












        cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 26 '17 at 17:37









        Emilio Galarraga

        32628




        32628



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394575%2fhow-to-print-of-the-text-between-the-last-occurence-of-a-pair-of-patterns%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