I have a complicated string that I need to search for and replace

I need to replace:




I'm afraid that I can't use python or any other scripting language (my bash script is currently 100+ lines)

So far I have a test file that contains the string in question and I am trying to use this sed command:

sed -i 's:common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/*.jar:#common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/*.jar\ncommon.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/*.jar,${catalina.home}/lib/ext,${catalina.home}/lib/ext/*.jar:' workers.properties

No errors are thrown, but nothing gets replaced either. Do I need to escape certain characters inside the string?

Any advice would help, thanks!

UPDATE: The command I used was similar to Joe's:

sed -i -e 's:^\(common.loader=${catalina.base}/lib,${catalina.base}/lib/\*\.jar,${catalina.home}/lib,${catalina.home}/lib/\*\.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/\*\.jar$\):#\1\n\1,${catalina.home}/lib/ext,${catalina.home}/lib/ext/*.jar:' /etc/tomcat6/catalina.properties
  • Do you have many different lines that begin "common.loader" ? – Iain May 16 '11 at 20:54
  • Actually, thats the only line that begins with 'common.loader' – Doug Molineux May 16 '11 at 21:04

You need to escape the '*' for it to work, you will probably want to escape the '.' for a more accurate match, the period will match any character, and the asterisk with match between any number of characters.

This will do the match you need:

sed -e 's:^\(common.loader=${catalina.base}/lib,${catalina.base}/lib/\*\.jar,${catalina.home}/lib,${catalina.home}/lib/\*\.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/\*\.jar$\):#\1,${catalina.home}/lib/ext,${catalina.home}/lib/ext/*.jar:' workers.properties

I escaped each meta-character (. and *) added anchors (^ and $) and used a back-reference with ( and ) so the old part of the pattern could be resused.

This is a decent guide to sed: http://www.grymoire.com/Unix/Sed.html

  • I changed it a little bit, but this helped me a lot, thanks! – Doug Molineux May 16 '11 at 21:40
  • 1
    @PeteHerbertPenito: for completeness can you post the solution as either an edit to Joe's answer or to your question please? – Iain May 17 '11 at 6:22
  • @Iain - added it to my question, cheers! – Doug Molineux May 17 '11 at 15:13

This will do it - create a file with the following sed commands in e.g. sed.script

    /^common\.loader/ {
    a common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}    /lib,${catalina.home}/lib/*.jar,/var/lib/tomcat6/common/classes,/var/lib/tomcat6/common/*.jar,${catalina.home}/lib/ext,${catalina.home}/lib/ext/*.jar

then use it like this

sed -i -f sed.script filetoedit

you may want to make a copy of the file so you can call it like so

sed -i.bak -f sed.script filetoedit

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.