replace a string with content from another file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

replace a string with content from another file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2006
afatguy afatguy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 21 October 2007, 10:48 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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,
Sponsored Links
    #2  
Old 09-14-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 16 Times in 16 Posts
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-14-2006 at 01:03 AM..
Sponsored Links
    #3  
Old 09-14-2006
afatguy afatguy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 21 October 2007, 10:48 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
thanks you
    #4  
Old 09-14-2006
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 22 April 2014, 1:35 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA
Sponsored Links
    #5  
Old 09-14-2006
afatguy afatguy is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 21 October 2007, 10:48 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
you're the best, thank you very much
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to replace particular content in a xml file cnraja Shell Programming and Scripting 4 01-29-2012 11:15 PM
Replace string by file content dim_nsk Shell Programming and Scripting 6 10-14-2011 11:26 PM
Replace file content after checking value varunrbs Shell Programming and Scripting 3 05-19-2010 11:04 PM
How to replace some content of a file and write with a new name mwrg Shell Programming and Scripting 5 11-15-2009 03:29 PM
SED to replace file content godfreyyip Shell Programming and Scripting 6 09-04-2009 06:24 AM



All times are GMT -4. The time now is 08:11 AM.