The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search and replace a specific text in text file? santosham UNIX for Dummies Questions & Answers 4 06-25-2008 05:53 PM
How to search and replace text in same file Vrgurav Shell Programming and Scripting 1 04-25-2008 06:20 AM
automating file search and replace text ommatidia Shell Programming and Scripting 3 02-28-2008 04:40 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-06-2008
kkraja kkraja is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 10
how can search a String in one text file and replace the whole line in another file

i am very new to UNIX

plz help me in this scenario

i have two text files as below

file1.txt

name=Rajakumar.
Discipline=Electronics and communication.
Designation=software Engineer.

file2.txt

name=Kannan.
Discipline=Mechanical.
Designation=CADD Design Engineer.

name=Ravi.
Discipline=Metallurgy.
Designation=Quality Analyst

i want the output be like this in file2.txt

name=Rajakumar.
Discipline=Electronics and communication.
Designation=software Engineer.

name=Ravi.
Discipline=Metallurgy.
Designation=Quality Analyst

i want to search for the string in one text file and search for the same in next file if it exist i want to repace the whole line with the line inwhich String exist in the first file as like the above files.

please help me to solve this by using shell scripting And SED commands.

Thanks in Advance,

Last edited by kkraja; 08-06-2008 at 02:24 AM..
  #2 (permalink)  
Old 08-06-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
How do you know which ones to replace?

i.e. file1.txt contains name=Rajakumar, which you have used to replace the first occurrence of name=Kannan, however for Discipline you have replaced the last occurrence. There is no way you can programme this unless there are clear rules to follow.
  #3 (permalink)  
Old 08-06-2008
kkraja kkraja is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 10
i want to replace the line which contains kannan with the line of the reference file as name=Rajakumar
  #4 (permalink)  
Old 08-06-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Something like this?

Code:
sed '/Kannan/{r file1.txt
N;N;d;}' file2.txt
  #5 (permalink)  
Old 08-06-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Why should this thread be in "Members Only" forum?
  #6 (permalink)  
Old 08-06-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by matrixmadhan View Post
Why should this thread be in "Members Only" forum?
Moved to UNIX for Dummies Questions & Answers.
  #7 (permalink)  
Old 08-06-2008
kmuqalled kmuqalled is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Use this command

sed 's/Old/New/g' file2
Closed Thread

Bookmarks

Tags
unix commands

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0