# replace a string with content from another file

**URL:** <https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122>\
**Category:** Shell Programming and Scripting\
**Created:** [September 13, 2006, 10:18pm UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122 "2006-09-13T22:18:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![afatguy](https://community.unix.com/letter_avatar/afatguy/32/6_22734026db12803ebb3e059622ac3534.png) [@afatguy](https://community.unix.com/u/afatguy)\
**Post date:** [September 13, 2006, 10:18pm UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122/1 "2006-09-13T22:18:25Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_22734026db12803ebb3e059622ac3534.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 14, 2006, 12:46am UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122/2 "2006-09-14T00:46:24Z")

</div>

while a shell solution is on the way , here is an alternative, in Python

```nohighlight
>>> 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

---

<div class="post-metadata">

**Author:** ![afatguy](https://community.unix.com/letter_avatar/afatguy/32/6_22734026db12803ebb3e059622ac3534.png) [@afatguy](https://community.unix.com/u/afatguy)\
**Post date:** [September 14, 2006, 1:52am UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122/3 "2006-09-14T01:52:33Z")

</div>

thanks you

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [September 14, 2006, 11:45am UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122/4 "2006-09-14T11:45:09Z")

</div>

str=xxxx  
sed -e "/$str/r FileB" -e "/$str/d" FileA

---

<div class="post-metadata">

**Author:** ![afatguy](https://community.unix.com/letter_avatar/afatguy/32/6_22734026db12803ebb3e059622ac3534.png) [@afatguy](https://community.unix.com/u/afatguy)\
**Post date:** [September 14, 2006, 9:25pm UTC](https://community.unix.com/t/replace-a-string-with-content-from-another-file/165122/5 "2006-09-14T21:25:29Z")

</div>

you're the best, thank you very much
