Print text between two patterns not containing a particular word

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I want to print some text between two patterns which doesn't contain a particular word



input text is



HEADER asdf asd 
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs


output needed is



HEADER asdf asd
asd COW assd
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs



conceptually something like this is needed



awk '/HEADER/,!/DOG/,TAIL' text 









share|improve this question
























  • The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

    – don_crissti
    May 21 '15 at 13:37











  • Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

    – Stéphane Chazelas
    May 21 '15 at 15:39











  • HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

    – user3905367
    May 22 '15 at 3:22

















1















I want to print some text between two patterns which doesn't contain a particular word



input text is



HEADER asdf asd 
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs


output needed is



HEADER asdf asd
asd COW assd
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs



conceptually something like this is needed



awk '/HEADER/,!/DOG/,TAIL' text 









share|improve this question
























  • The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

    – don_crissti
    May 21 '15 at 13:37











  • Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

    – Stéphane Chazelas
    May 21 '15 at 15:39











  • HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

    – user3905367
    May 22 '15 at 3:22













1












1








1


1






I want to print some text between two patterns which doesn't contain a particular word



input text is



HEADER asdf asd 
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs


output needed is



HEADER asdf asd
asd COW assd
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs



conceptually something like this is needed



awk '/HEADER/,!/DOG/,TAIL' text 









share|improve this question
















I want to print some text between two patterns which doesn't contain a particular word



input text is



HEADER asdf asd 
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs


output needed is



HEADER asdf asd
asd COW assd
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs



conceptually something like this is needed



awk '/HEADER/,!/DOG/,TAIL' text 






text-processing sed awk text






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 13:23









Rui F Ribeiro

41.9k1483142




41.9k1483142










asked May 21 '15 at 13:18









user3905367user3905367

111




111












  • The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

    – don_crissti
    May 21 '15 at 13:37











  • Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

    – Stéphane Chazelas
    May 21 '15 at 15:39











  • HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

    – user3905367
    May 22 '15 at 3:22

















  • The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

    – don_crissti
    May 21 '15 at 13:37











  • Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

    – Stéphane Chazelas
    May 21 '15 at 15:39











  • HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

    – user3905367
    May 22 '15 at 3:22
















The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

– don_crissti
May 21 '15 at 13:37





The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...

– don_crissti
May 21 '15 at 13:37













Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

– Stéphane Chazelas
May 21 '15 at 15:39





Can you HEAD or TAIL lines contain DOG? If yes, is that ground for exclusion?

– Stéphane Chazelas
May 21 '15 at 15:39













HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

– user3905367
May 22 '15 at 3:22





HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.

– user3905367
May 22 '15 at 3:22










2 Answers
2






active

oldest

votes


















2














With perl:



perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file


With awk:



awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file





share|improve this answer
































    2














    If there is no other limitation here your script



    sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text 


    Just for fun other variants of awk:

    one:



    awk '
    BEGINprn=1
    /^HEADER/block=1
    block
    if(/DOG/)
    prn=0
    if(!/^TAIL/)
    line=line $0 "n"
    next


    prnprint line $0
    /^TAIL/
    block=0
    prn=1
    line=""

    ' text


    two:



    awk '
    /^HEADER/
    line=$0
    while(!/TAIL/)
    getline
    line=line "n" $0

    if(line !~ /DOG/)
    print line
    next

    1' text





    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%2f204822%2fprint-text-between-two-patterns-not-containing-a-particular-word%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









      2














      With perl:



      perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file


      With awk:



      awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
      /DOG/skip = 1; next
      ! skip content=content $0 "n"
      /^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file





      share|improve this answer





























        2














        With perl:



        perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file


        With awk:



        awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
        /DOG/skip = 1; next
        ! skip content=content $0 "n"
        /^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file





        share|improve this answer



























          2












          2








          2







          With perl:



          perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file


          With awk:



          awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
          /DOG/skip = 1; next
          ! skip content=content $0 "n"
          /^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file





          share|improve this answer















          With perl:



          perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file


          With awk:



          awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
          /DOG/skip = 1; next
          ! skip content=content $0 "n"
          /^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 21 '15 at 14:32

























          answered May 21 '15 at 13:58









          Stéphane ChazelasStéphane Chazelas

          313k57592948




          313k57592948























              2














              If there is no other limitation here your script



              sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text 


              Just for fun other variants of awk:

              one:



              awk '
              BEGINprn=1
              /^HEADER/block=1
              block
              if(/DOG/)
              prn=0
              if(!/^TAIL/)
              line=line $0 "n"
              next


              prnprint line $0
              /^TAIL/
              block=0
              prn=1
              line=""

              ' text


              two:



              awk '
              /^HEADER/
              line=$0
              while(!/TAIL/)
              getline
              line=line "n" $0

              if(line !~ /DOG/)
              print line
              next

              1' text





              share|improve this answer





























                2














                If there is no other limitation here your script



                sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text 


                Just for fun other variants of awk:

                one:



                awk '
                BEGINprn=1
                /^HEADER/block=1
                block
                if(/DOG/)
                prn=0
                if(!/^TAIL/)
                line=line $0 "n"
                next


                prnprint line $0
                /^TAIL/
                block=0
                prn=1
                line=""

                ' text


                two:



                awk '
                /^HEADER/
                line=$0
                while(!/TAIL/)
                getline
                line=line "n" $0

                if(line !~ /DOG/)
                print line
                next

                1' text





                share|improve this answer



























                  2












                  2








                  2







                  If there is no other limitation here your script



                  sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text 


                  Just for fun other variants of awk:

                  one:



                  awk '
                  BEGINprn=1
                  /^HEADER/block=1
                  block
                  if(/DOG/)
                  prn=0
                  if(!/^TAIL/)
                  line=line $0 "n"
                  next


                  prnprint line $0
                  /^TAIL/
                  block=0
                  prn=1
                  line=""

                  ' text


                  two:



                  awk '
                  /^HEADER/
                  line=$0
                  while(!/TAIL/)
                  getline
                  line=line "n" $0

                  if(line !~ /DOG/)
                  print line
                  next

                  1' text





                  share|improve this answer















                  If there is no other limitation here your script



                  sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text 


                  Just for fun other variants of awk:

                  one:



                  awk '
                  BEGINprn=1
                  /^HEADER/block=1
                  block
                  if(/DOG/)
                  prn=0
                  if(!/^TAIL/)
                  line=line $0 "n"
                  next


                  prnprint line $0
                  /^TAIL/
                  block=0
                  prn=1
                  line=""

                  ' text


                  two:



                  awk '
                  /^HEADER/
                  line=$0
                  while(!/TAIL/)
                  getline
                  line=line "n" $0

                  if(line !~ /DOG/)
                  print line
                  next

                  1' text






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 21 '15 at 15:41

























                  answered May 21 '15 at 14:44









                  CostasCostas

                  12.7k1129




                  12.7k1129



























                      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%2f204822%2fprint-text-between-two-patterns-not-containing-a-particular-word%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