sed is appending next line after replacing first line

I want to replace USERNAME and PASSWORD in following properties files code using sed / awk

jdbc.connection.driver_class=com.ibm.as400.access.AS400JDBCDriver
jdbc.connection.username=USERNAME
jdbc.connection.password=PASSWORD

jdbc.minPoolSize=1
jdbc.maxPoolSize=15

I tried it using the following script

sed -i -e "s/^${JDBC_USER_KEY}=.*/${JDBC_USER_KEY}=${DATABASE_USER_NAME}/" 
       -e "s/^${JDBC_PASSWORD_KEY}=.*/${JDBC_PASSWORD_KEY}=${DATABASE_PASSWORD}/"  database.properties

Where :

JDBC_USER_KEY=jdbc.connection.username
DATABASE_USER_NAME=TEST
JDBC_PASSWORD_KEY=jdbc.connection.password
DATABASE_PASSWORD=TEST

But, the output is :

jdbc.connection.driver_class=com.ibm.as400.access.AS400JDBCDriver
jdbc.connection.username=TESTjdbc.connection.password=TEST

jdbc.minPoolSize=1
jdbc.maxPoolSize=15

The value has been replaced but the next line “jdbc.connection.password=TEST” has been appeneded with previous line.

I searched a lot on Google but did not find any solution/s.

I also tried awk (See the following):

awk 'BEGIN{ FS="=";OFS="=" } {if($1==pname) $2=newval; print $0,"n"}' pname="$property_name" newval="$new_value" $ARTIFACTS_HOME/properties/$1 >> $ARTIFACTS_HOME/properties/test.properties

Where $property_name is jdbc.connection.username and $new_value is TEST.

Please help!


Source: shell

Leave a Reply