![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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, |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
bor bor bor something something something bib bib bi Last edited by ghostdog74; 09-13-2006 at 10:03 PM. |
|
#3
|
|||
|
|||
|
thanks you
|
|
#4
|
|||
|
|||
|
str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA |
|
#5
|
|||
|
|||
|
you're the best, thank you very much
|
|||
| Google The UNIX and Linux Forums |