Ajit Nair
Ajit Nair

Reputation: 455

How to change contents of a file in shell script?

I am creating a script script1.sh through which I have to change an xml file(abc.xml) contents. The contents of xml file are

area = xyz/a
url = "sqlserver://servername:portno/a"
username=""
password=""

area = xyz/b
url = "sqlserver://servername:portno/b"
username=""
password=""

I have to change servername, portno, username and password for both area using script1.sh. Servername and portno will be same for both area but username and password will differ. So how can I make changes from script1.sh to abc.xml

sed -i 's/$strurl/$url/g' context.sh

This is how I am searching but can't search and change url rest all are fine. I think the url string is too big to be searched.

Upvotes: 1

Views: 3472

Answers (2)

Alfe
Alfe

Reputation: 59426

You can use sed with option -i to change a file in-place.

sed -i 's/what/towhat/g' filename.txt

In case your what contains slashes (/) itself, use any other character (e. g. ,) which does not appear in the what or the towhat.

Upvotes: 0

Ajit Nair
Ajit Nair

Reputation: 455

sed -i 's!$strurl!$url!g' filename

This will be the answer as url itself contains '/' as separator so we need to use some other separator inside sed command

Upvotes: 2

Related Questions