Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:'

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/
share|improve this question
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
up vote 2 down vote accepted

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:'

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:

share|improve this answer
I changed it a little bit, but this helped me a lot, thanks! – Doug Molineux May 16 '11 at 21:40
@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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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