The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace character in a string pattern and save the change in same file mihir0011 Shell Programming and Scripting 2 09-26-2007 02:31 PM
How to replace the string in unix file? param_it UNIX for Dummies Questions & Answers 5 06-26-2007 06:06 AM
replace a string in a file ratan2204 Shell Programming and Scripting 7 05-10-2006 06:40 AM
Replace all occurances of a string in all file-/foldernames, recursively TheMJ Shell Programming and Scripting 2 04-11-2006 10:40 PM
replace string in binary file sg1207 Shell Programming and Scripting 2 07-28-2004 08:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
replace a string with content from another file

Hi,
I'm a newbi in shell script. Here what I want to do:

FileA:
bor bor bor
xxxx
bib bib bi

FileB:

something something
something

I want to replace string "xxxx" in FileA with contents of FileB.
i tried with sed:

fileb=`cat FileB`
reg=xxxx
file=FileA

sed -e "s+$reg+$fileb+g" $file

but it said:
sed: Function s+reg+ cannot be parsed.

thanks,
Reply With Quote
Forum Sponsor
  #2  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
while a shell solution is on the way , here is an alternative, in Python

Code:
>>> import fileinput
>>> Bcontents = open("fileB").read() #fileB contents stored as string
>>> for lines in fileinput.FileInput("fileA",inplace=1): #inplace editing of fileA
... 	lines = lines.strip() #get rid of newlines
... 	if lines.startswith("xxxx"):
... 		lines = Bcontents
... 	print lines
In fileA:

bor bor bor
something something
something
bib bib bi

Last edited by ghostdog74; 09-13-2006 at 10:03 PM.
Reply With Quote
  #3  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
thanks you
Reply With Quote
  #4  
Old 09-14-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA
Reply With Quote
  #5  
Old 09-14-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
you're the best, thank you very much
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0