Maurizio
Maurizio

Reputation: 189

SHELL - SED replacing text between " "

I'm new at shell and SED in general. I am trying to replace a text in a file. This text is between " " and i am struggling to create the command...

The file has something like: download_amule_tcp_port="42450"

My code:

TCPPort=44444

sed -i 's/^download_amule_tcp_port=\".*/download_amule_tcp_port=\"$TCPPort\"/' settings.conf

It is not working...

download_amule_tcp_port=$TCPPort"

Thanks for any help!

M

Upvotes: 0

Views: 968

Answers (3)

steffen
steffen

Reputation: 16948

Use double quotes instead of single quotes and escape the inner double quotes:

sed -i "s/^download_amule_tcp_port=\".*/download_amule_tcp_port=\"$TCPPORT\"/" settings.conf

Btw, you set the variable TCPPort instead of TCPPORT.

Upvotes: 0

asxalex
asxalex

Reputation: 29

the problem lies in the single quote, which prevents the variable,$TCPPort in this case, from being expanded to "44444".

the following code should work

TCPPort="44444"
sed -i 's/^download_amule_tcp_port=".*/download_amule_tcp_port="'"$TCPPort"'"/g' settings.conf

Upvotes: 1

Maurizio
Maurizio

Reputation: 189

==== EDIT ==== The correct syntax as in comments, should be:

TCPPort="44444"

sed -i "s/^download_amule_tcp_port=.*/download_amule_tcp_port=\"$TCPPo
rt\"/" settings.conf

This will work. thanks!

Upvotes: 1

Related Questions