user3056318
user3056318

Reputation: 1

Regular expression required for replacing string in shell script

Can anyone please help me write a shell script in linux which would replace the hostname in a particular file.

eg : I have multiple files which have certain ip addresses.

http://10.160.228.12:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL

http://VQAIAAPPDEV:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL

Basically what I would want to replace is the string between "http://" and ":8001" with any required string.

Can someone help me with this please.

Some More info:-

I want to do this iteratively across many folders. So basically it will search all the files in each folder and perform the necessary changes.

Upvotes: 0

Views: 140

Answers (3)

Ranjithkumar T
Ranjithkumar T

Reputation: 1956

Tried with "for"

 # cat replace.txt
 http://10.160.228.12:8001/soa-     infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL

 http://VQAIAAPPDEV:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL


 # for i in `cat replace.txt | awk -F: '{print $2}' | sed 's/^\/\///g' | sed '/^$/d'` ; do sed -i "s/$i/Your_hostname/"  replace.txt ; done

 # cat replace.txt
 http://Your_hostname:8001/soa- infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL

 http://Your_hostname:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL

Its working for me...!

Upvotes: 0

AlexiusFlavius
AlexiusFlavius

Reputation: 114

Use sed command from Linux shell

sed -i 's%OldHost%NewHost%g' /yourfolder/yourfile

Upvotes: 0

devnull
devnull

Reputation: 123448

You could use sed. Saying:

sed -r 's|(http://)([^:]*)(:8001)|\1something\3|g' filename

would replace is the string between "http://" and ":8001" with something.

If you want to make the change to the file in-place, use the -i option:

sed -i -r 's|(http://)([^:]*)(:8001)|\1something\3|g' filename

Upvotes: 2

Related Questions