Disable Tomcat Session persistence using sed or perl












1














I need to disable Tomcat session persistence as I have implemented SpringSession on my Spring (not Spring Boot) Application.



I've tried using sed and perl to edit multiple lines in the context.xml file, but none of them have worked. I've tried the following, which do not throw any error, but don't actually do anything:



sed -i 's/<!--n<Manager pathname="" />n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml

perl -i -0pe 's/<!--n<Manager pathname="" /> n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml


Essentially I want to un-comment the Manager pathname line in context.xml



I want to change:



<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<!--
<Manager pathname="" />
-->


to



<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Manager pathname="" />









share|improve this question







New contributor




DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1














    I need to disable Tomcat session persistence as I have implemented SpringSession on my Spring (not Spring Boot) Application.



    I've tried using sed and perl to edit multiple lines in the context.xml file, but none of them have worked. I've tried the following, which do not throw any error, but don't actually do anything:



    sed -i 's/<!--n<Manager pathname="" />n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml

    perl -i -0pe 's/<!--n<Manager pathname="" /> n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml


    Essentially I want to un-comment the Manager pathname line in context.xml



    I want to change:



    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <!--
    <Manager pathname="" />
    -->


    to



    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <Manager pathname="" />









    share|improve this question







    New contributor




    DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1







      I need to disable Tomcat session persistence as I have implemented SpringSession on my Spring (not Spring Boot) Application.



      I've tried using sed and perl to edit multiple lines in the context.xml file, but none of them have worked. I've tried the following, which do not throw any error, but don't actually do anything:



      sed -i 's/<!--n<Manager pathname="" />n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml

      perl -i -0pe 's/<!--n<Manager pathname="" /> n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml


      Essentially I want to un-comment the Manager pathname line in context.xml



      I want to change:



      <!-- Uncomment this to disable session persistence across Tomcat restarts -->

      <!--
      <Manager pathname="" />
      -->


      to



      <!-- Uncomment this to disable session persistence across Tomcat restarts -->

      <Manager pathname="" />









      share|improve this question







      New contributor




      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I need to disable Tomcat session persistence as I have implemented SpringSession on my Spring (not Spring Boot) Application.



      I've tried using sed and perl to edit multiple lines in the context.xml file, but none of them have worked. I've tried the following, which do not throw any error, but don't actually do anything:



      sed -i 's/<!--n<Manager pathname="" />n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml

      perl -i -0pe 's/<!--n<Manager pathname="" /> n-->/<Manager pathname="" />/' /opt/tomcat/conf/context.xml


      Essentially I want to un-comment the Manager pathname line in context.xml



      I want to change:



      <!-- Uncomment this to disable session persistence across Tomcat restarts -->

      <!--
      <Manager pathname="" />
      -->


      to



      <!-- Uncomment this to disable session persistence across Tomcat restarts -->

      <Manager pathname="" />






      sed perl tomcat






      share|improve this question







      New contributor




      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Jan 2 at 16:58









      DaithiGDaithiG

      61




      61




      New contributor




      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          0














          sed and perl by default operate on lines; your problem spans multiple lines. Therefore no match is possible on your input by default. Two common workarounds are either to operate on the entire file at once or to use a state machine that operates by line but keeps track of what has been seen. The entire file method, looking for your data in your input file and keeping only the desired bit:



          $ perl -0777 -ple 's{<!--s*(<Manager pathname="" />)s*-->}{$1}' input
          <!-- Uncomment this to disable session persistence across Tomcat restarts -->

          <Manager pathname="" />
          $


          The state machine method is more complicated as there may be other comments that must remain unmolested, and EOF may need special handling if the pattern runs up against that or not; this version prints previously seen lines unless the line is the one we want, in which case the next line is also skipped. You would ideally want unit tests to cover any edge cases possible with such code.



          $ perl -nle 'if (m{<Manager pathname="" />}) { readline } else { print $prev } $prev=$_; END{ print $prev }' input

          <!-- Uncomment this to disable session persistence across Tomcat restarts -->

          <Manager pathname="" />
          $


          A diff should probably be done afterwards to confirm the edits have changed only what is desired, and then possibly patch used to apply that diff to future editions of the file, which has the advantage of warning about the inability to patch the file and some means to recover from slightly different files.



          $ perl -i.old ..
          $ diff input.old input > input.patch


          A third method would be to fully parse the XML into a tree form in memory and then manipulate that tree to the desired new state and then emit that new form. This has the advantage of being less likely to be thrown off by, say, random whitespace or other changes that cause the above regular expressions to not match (that is, the above methods are more fragile) but will be more complicated to setup, and depending on the parser and emitter used may introduce undesirable changes to the input file.






          share|improve this answer





























            0














            Thanks thrig, that was an interesting read and some handy tips for any future perl use.



            Unfortunately I needed a quick solution, so I stored the modified context.xml file in source control, and ran an ansible scripts to checkout the updated file from svn and restart the server.



            Which is very much what your fist suggestion is, and is in retrospect probably the best solution.



            Thanks again.





            share








            New contributor




            DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















              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
              });


              }
              });






              DaithiG is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492048%2fdisable-tomcat-session-persistence-using-sed-or-perl%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









              0














              sed and perl by default operate on lines; your problem spans multiple lines. Therefore no match is possible on your input by default. Two common workarounds are either to operate on the entire file at once or to use a state machine that operates by line but keeps track of what has been seen. The entire file method, looking for your data in your input file and keeping only the desired bit:



              $ perl -0777 -ple 's{<!--s*(<Manager pathname="" />)s*-->}{$1}' input
              <!-- Uncomment this to disable session persistence across Tomcat restarts -->

              <Manager pathname="" />
              $


              The state machine method is more complicated as there may be other comments that must remain unmolested, and EOF may need special handling if the pattern runs up against that or not; this version prints previously seen lines unless the line is the one we want, in which case the next line is also skipped. You would ideally want unit tests to cover any edge cases possible with such code.



              $ perl -nle 'if (m{<Manager pathname="" />}) { readline } else { print $prev } $prev=$_; END{ print $prev }' input

              <!-- Uncomment this to disable session persistence across Tomcat restarts -->

              <Manager pathname="" />
              $


              A diff should probably be done afterwards to confirm the edits have changed only what is desired, and then possibly patch used to apply that diff to future editions of the file, which has the advantage of warning about the inability to patch the file and some means to recover from slightly different files.



              $ perl -i.old ..
              $ diff input.old input > input.patch


              A third method would be to fully parse the XML into a tree form in memory and then manipulate that tree to the desired new state and then emit that new form. This has the advantage of being less likely to be thrown off by, say, random whitespace or other changes that cause the above regular expressions to not match (that is, the above methods are more fragile) but will be more complicated to setup, and depending on the parser and emitter used may introduce undesirable changes to the input file.






              share|improve this answer


























                0














                sed and perl by default operate on lines; your problem spans multiple lines. Therefore no match is possible on your input by default. Two common workarounds are either to operate on the entire file at once or to use a state machine that operates by line but keeps track of what has been seen. The entire file method, looking for your data in your input file and keeping only the desired bit:



                $ perl -0777 -ple 's{<!--s*(<Manager pathname="" />)s*-->}{$1}' input
                <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                <Manager pathname="" />
                $


                The state machine method is more complicated as there may be other comments that must remain unmolested, and EOF may need special handling if the pattern runs up against that or not; this version prints previously seen lines unless the line is the one we want, in which case the next line is also skipped. You would ideally want unit tests to cover any edge cases possible with such code.



                $ perl -nle 'if (m{<Manager pathname="" />}) { readline } else { print $prev } $prev=$_; END{ print $prev }' input

                <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                <Manager pathname="" />
                $


                A diff should probably be done afterwards to confirm the edits have changed only what is desired, and then possibly patch used to apply that diff to future editions of the file, which has the advantage of warning about the inability to patch the file and some means to recover from slightly different files.



                $ perl -i.old ..
                $ diff input.old input > input.patch


                A third method would be to fully parse the XML into a tree form in memory and then manipulate that tree to the desired new state and then emit that new form. This has the advantage of being less likely to be thrown off by, say, random whitespace or other changes that cause the above regular expressions to not match (that is, the above methods are more fragile) but will be more complicated to setup, and depending on the parser and emitter used may introduce undesirable changes to the input file.






                share|improve this answer
























                  0












                  0








                  0






                  sed and perl by default operate on lines; your problem spans multiple lines. Therefore no match is possible on your input by default. Two common workarounds are either to operate on the entire file at once or to use a state machine that operates by line but keeps track of what has been seen. The entire file method, looking for your data in your input file and keeping only the desired bit:



                  $ perl -0777 -ple 's{<!--s*(<Manager pathname="" />)s*-->}{$1}' input
                  <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                  <Manager pathname="" />
                  $


                  The state machine method is more complicated as there may be other comments that must remain unmolested, and EOF may need special handling if the pattern runs up against that or not; this version prints previously seen lines unless the line is the one we want, in which case the next line is also skipped. You would ideally want unit tests to cover any edge cases possible with such code.



                  $ perl -nle 'if (m{<Manager pathname="" />}) { readline } else { print $prev } $prev=$_; END{ print $prev }' input

                  <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                  <Manager pathname="" />
                  $


                  A diff should probably be done afterwards to confirm the edits have changed only what is desired, and then possibly patch used to apply that diff to future editions of the file, which has the advantage of warning about the inability to patch the file and some means to recover from slightly different files.



                  $ perl -i.old ..
                  $ diff input.old input > input.patch


                  A third method would be to fully parse the XML into a tree form in memory and then manipulate that tree to the desired new state and then emit that new form. This has the advantage of being less likely to be thrown off by, say, random whitespace or other changes that cause the above regular expressions to not match (that is, the above methods are more fragile) but will be more complicated to setup, and depending on the parser and emitter used may introduce undesirable changes to the input file.






                  share|improve this answer












                  sed and perl by default operate on lines; your problem spans multiple lines. Therefore no match is possible on your input by default. Two common workarounds are either to operate on the entire file at once or to use a state machine that operates by line but keeps track of what has been seen. The entire file method, looking for your data in your input file and keeping only the desired bit:



                  $ perl -0777 -ple 's{<!--s*(<Manager pathname="" />)s*-->}{$1}' input
                  <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                  <Manager pathname="" />
                  $


                  The state machine method is more complicated as there may be other comments that must remain unmolested, and EOF may need special handling if the pattern runs up against that or not; this version prints previously seen lines unless the line is the one we want, in which case the next line is also skipped. You would ideally want unit tests to cover any edge cases possible with such code.



                  $ perl -nle 'if (m{<Manager pathname="" />}) { readline } else { print $prev } $prev=$_; END{ print $prev }' input

                  <!-- Uncomment this to disable session persistence across Tomcat restarts -->

                  <Manager pathname="" />
                  $


                  A diff should probably be done afterwards to confirm the edits have changed only what is desired, and then possibly patch used to apply that diff to future editions of the file, which has the advantage of warning about the inability to patch the file and some means to recover from slightly different files.



                  $ perl -i.old ..
                  $ diff input.old input > input.patch


                  A third method would be to fully parse the XML into a tree form in memory and then manipulate that tree to the desired new state and then emit that new form. This has the advantage of being less likely to be thrown off by, say, random whitespace or other changes that cause the above regular expressions to not match (that is, the above methods are more fragile) but will be more complicated to setup, and depending on the parser and emitter used may introduce undesirable changes to the input file.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 21:06









                  thrigthrig

                  24.3k23056




                  24.3k23056

























                      0














                      Thanks thrig, that was an interesting read and some handy tips for any future perl use.



                      Unfortunately I needed a quick solution, so I stored the modified context.xml file in source control, and ran an ansible scripts to checkout the updated file from svn and restart the server.



                      Which is very much what your fist suggestion is, and is in retrospect probably the best solution.



                      Thanks again.





                      share








                      New contributor




                      DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.























                        0














                        Thanks thrig, that was an interesting read and some handy tips for any future perl use.



                        Unfortunately I needed a quick solution, so I stored the modified context.xml file in source control, and ran an ansible scripts to checkout the updated file from svn and restart the server.



                        Which is very much what your fist suggestion is, and is in retrospect probably the best solution.



                        Thanks again.





                        share








                        New contributor




                        DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





















                          0












                          0








                          0






                          Thanks thrig, that was an interesting read and some handy tips for any future perl use.



                          Unfortunately I needed a quick solution, so I stored the modified context.xml file in source control, and ran an ansible scripts to checkout the updated file from svn and restart the server.



                          Which is very much what your fist suggestion is, and is in retrospect probably the best solution.



                          Thanks again.





                          share








                          New contributor




                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          Thanks thrig, that was an interesting read and some handy tips for any future perl use.



                          Unfortunately I needed a quick solution, so I stored the modified context.xml file in source control, and ran an ansible scripts to checkout the updated file from svn and restart the server.



                          Which is very much what your fist suggestion is, and is in retrospect probably the best solution.



                          Thanks again.






                          share








                          New contributor




                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share


                          share






                          New contributor




                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 5 mins ago









                          DaithiGDaithiG

                          61




                          61




                          New contributor




                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          DaithiG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






















                              DaithiG is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              DaithiG is a new contributor. Be nice, and check out our Code of Conduct.













                              DaithiG is a new contributor. Be nice, and check out our Code of Conduct.












                              DaithiG is a new contributor. Be nice, and check out our Code of Conduct.
















                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f492048%2fdisable-tomcat-session-persistence-using-sed-or-perl%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

                              CARDNET

                              Boot-repair Failure: Unable to locate package grub-common:i386

                              濃尾地震