Use sed to print selected lines within a range

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











up vote
1
down vote

favorite












Consider the simplified file



AAA
BBB
CCC1
DDD
EEE
CCC2
DDD
FFF
GGG
CCC3
HHH


I can pick out the range EEE to FFF with



sed -n '/EEE/,/FFF/p'


Suppose though that I want to print any line containing C but only within the matching range. I can pipe the result from sed through grep



sed -n '/EEE/,/FFF/p' | grep 'C'


I could also do the range and match in a little awk script (or perl, python, etc.). But how would I do this using just one invocation of sed?










share|improve this question



























    up vote
    1
    down vote

    favorite












    Consider the simplified file



    AAA
    BBB
    CCC1
    DDD
    EEE
    CCC2
    DDD
    FFF
    GGG
    CCC3
    HHH


    I can pick out the range EEE to FFF with



    sed -n '/EEE/,/FFF/p'


    Suppose though that I want to print any line containing C but only within the matching range. I can pipe the result from sed through grep



    sed -n '/EEE/,/FFF/p' | grep 'C'


    I could also do the range and match in a little awk script (or perl, python, etc.). But how would I do this using just one invocation of sed?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Consider the simplified file



      AAA
      BBB
      CCC1
      DDD
      EEE
      CCC2
      DDD
      FFF
      GGG
      CCC3
      HHH


      I can pick out the range EEE to FFF with



      sed -n '/EEE/,/FFF/p'


      Suppose though that I want to print any line containing C but only within the matching range. I can pipe the result from sed through grep



      sed -n '/EEE/,/FFF/p' | grep 'C'


      I could also do the range and match in a little awk script (or perl, python, etc.). But how would I do this using just one invocation of sed?










      share|improve this question















      Consider the simplified file



      AAA
      BBB
      CCC1
      DDD
      EEE
      CCC2
      DDD
      FFF
      GGG
      CCC3
      HHH


      I can pick out the range EEE to FFF with



      sed -n '/EEE/,/FFF/p'


      Suppose though that I want to print any line containing C but only within the matching range. I can pipe the result from sed through grep



      sed -n '/EEE/,/FFF/p' | grep 'C'


      I could also do the range and match in a little awk script (or perl, python, etc.). But how would I do this using just one invocation of sed?







      sed pattern-matching






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      choroba

      24.9k34168




      24.9k34168










      asked 1 hour ago









      roaima

      40.6k547110




      40.6k547110




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          Use a block in which you tell sed to only print when it sees C:



          sed -n '/EEE/,/FFF//C/p'





          share|improve this answer



























            up vote
            2
            down vote













            You can try :



            sed '/EEE/,/FFF/!d;/C/!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%2f472241%2fuse-sed-to-print-selected-lines-within-a-range%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
              5
              down vote













              Use a block in which you tell sed to only print when it sees C:



              sed -n '/EEE/,/FFF//C/p'





              share|improve this answer
























                up vote
                5
                down vote













                Use a block in which you tell sed to only print when it sees C:



                sed -n '/EEE/,/FFF//C/p'





                share|improve this answer






















                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  Use a block in which you tell sed to only print when it sees C:



                  sed -n '/EEE/,/FFF//C/p'





                  share|improve this answer












                  Use a block in which you tell sed to only print when it sees C:



                  sed -n '/EEE/,/FFF//C/p'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  choroba

                  24.9k34168




                  24.9k34168






















                      up vote
                      2
                      down vote













                      You can try :



                      sed '/EEE/,/FFF/!d;/C/!d'





                      share|improve this answer
























                        up vote
                        2
                        down vote













                        You can try :



                        sed '/EEE/,/FFF/!d;/C/!d'





                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          You can try :



                          sed '/EEE/,/FFF/!d;/C/!d'





                          share|improve this answer












                          You can try :



                          sed '/EEE/,/FFF/!d;/C/!d'






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 59 mins ago









                          ctac_

                          1,111116




                          1,111116



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f472241%2fuse-sed-to-print-selected-lines-within-a-range%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery