Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and replace in a file using sed or awk

Status
Not open for further replies.

krishsays

IS-IT--Management
Dec 3, 2004
45
GB
Hi ,

I have to do a migration from staging to production. I have 50 files where I need to change the hostnames and urls to new environment hostnames and urls. Please can somebody help me in writing a script using which I can read a hostname1 from a file and then search in a second file and change it with new hostname which is also given in first file. I think we can use array for old and new hostnames , only problem is how to search a array value in the file and replace it with second array value.
Please help, thanks iin advance.

Thanks,
Krishan]
 
A starting point.
Say you have a file named changes.txt like this:
oldhostname=newhostname
oldurl1=newurl1
oldurl2=newurl2
...
Then you may consider a command like this:
awk -F'=' '{
NR==FNR{a[$1]=$2;next}
{x=$0;for(k in a)gsub(k,a[k],x);print x}
' changes.txt /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top