0

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.

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

What if I want to iteratively check in files in many folders and do the necessary changes?
@user3056318 Use find. You can say: find /some/path -type f -exec sed -i -r 's|(http://)([^:]*)(:8001)|\1something\3|g' {} \;
@user3056318 Consider accepting the answer if it worked for you. You might also want to refer to this.
0

Use sed command from Linux shell

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

Comments

0

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...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.