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="" />
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.