Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

replace a string with content from another file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 09-14-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 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 Unix and Linux 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 Unix and Linux 09-14-2006
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 28 August 2015, 5:16 AM EDT
Location: Bangalore,India
Posts: 2,139
Thanks: 15
Thanked 154 Times in 146 Posts
str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA
Sponsored Links
    #5  
Old Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:46 PM.