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 and shell scripting languages 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 06:31 PM
How to replace the string in unix file? param_it UNIX for Dummies Questions & Answers 5 06-26-2007 10:06 AM
replace a string in a file ratan2204 Shell Programming and Scripting 7 05-10-2006 10:40 AM
Replace all occurances of a string in all file-/foldernames, recursively TheMJ Shell Programming and Scripting 2 04-12-2006 02:40 AM
replace string in binary file sg1207 Shell Programming and Scripting 2 07-29-2004 12:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-13-2006
afatguy afatguy is offline
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,
  #2 (permalink)  
Old 09-14-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
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 02:03 AM..
  #3 (permalink)  
Old 09-14-2006
afatguy afatguy is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
thanks you
  #4 (permalink)  
Old 09-14-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA
  #5 (permalink)  
Old 09-14-2006
afatguy afatguy is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
you're the best, thank you very much
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0