1

I would like to replace some lines of tomcat server.xml file with sed command.

<Server port="8005" shutdown="SHUTDOWN">

for

<Server port="8205" shutdown="SHUTDOWN">

and

<Connector port="8080" protocol="HTTP/1.1

for

<Connector port="8280" protocol="HTTP/1.1

I tried to do this but tmp_server_2.xml is always empty:

sed -e "s/<Server port=\"8005\" shutdown=\"SHUTDOWN\">/<Server port=\"$short_port\" shutdown=\"SHUTDOWN\">/" /company/$tomcat_port/conf/server.xml > tmp_server.xml;
sed -e "s/<Connector port=\"8080\" protocol=\"HTTP/1.1\"/<Connector port=\"$port\" protocol=\"HTTP/1.1\"/" ./tmp_server.xml > ./tmp_server_2.xml;

Why is that ? How can I do that ?

0

1 Answer 1

4

sed doesn't understand your second command because you haven't escaped the slash here:

sed -e "s/<Connector port=\"8080\" protocol=\"HTTP/1.1\"/<Connector port=\"$port\" protocol=\"HTTP/1.1\"/" ./tmp_server.xml > ./tmp_server_2.xml
                                                  ^                                               ^

Hence:

sed: -e expression #1, char 46: unknown option to `s'

Since slashes are used as s/// delimiters, you have to escape them:

sed "s/<Connector port=\"8080\" protocol=\"HTTP\/1\.1/<Connector port=\"$port\" protocol=\"HTTP\/1.1\"/"

But it would be easier if you just used another separator. You can also use single quotes to avoid escaping double quotes, but you can't expand variables in single quotes, so:

sed 's:<Connector port="8080" protocol="HTTP/1\.1:<Connector port="'"$port"'" protocol="HTTP/1.1":'

The dot (.) is escaped because it's a special character that matches any character.

0

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