|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
find and replace in a file using awk or SED
Hi
I want to create a script which reads string1 from file1 searches that string1 in file2 and then replaces it with string2 which also there in file1. Actually we are doing a migration and in this i need to replace lot server names and with new names and i want to take read old server name from a file search it in the file and then replace it with new server name. Please help me . Thanks, krish |
|
#2
|
|||
|
|||
|
both sed and awk can do the job. i prefer sed.
supposed list1 has the format: oldname#newname (you gave no exemple) i suggest: a sed cmd translating the list1 in a sed cmd file, exec it, let have a look Code:
sed 's/\(.*\)#\(.*\)/ \/\1\/s\/\/\2\/g/g' inputfile >sedcmd this will translate old#new to /old/s//new/g which is a sed statement replacing all occ of 'old' by 'new' now executing sed -f sedcmd file2 > newserversnames does the job. remember, sed has limits, i believe (not sure) 200 cmds a file so if your file1 if >200 lines, after the first sed you must split 'sedcmd' and use a cascade sed -f sed1 file1¦sed -f sed2¦sed -f sed3 ..... >output see man pages of 'split' and 'sed' |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > find and replace in a file using awk or SED |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|