UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 3rd, 2004, 05:15 PM
krishsays krishsays is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 krishsays User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old December 4th, 2004, 04:44 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
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'

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > find and replace in a file using awk or SED


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway