writing variable contents to a specific line in a file

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 19
Reputation: kneiel is an unknown quantity at this point 
Solved Threads: 0
kneiel kneiel is offline Offline
Newbie Poster

writing variable contents to a specific line in a file

 
-1
  #1
Sep 23rd, 2009
if line 5 of a file needs to be replaced with contents of $line, how could we possibly do this using sed ?
Last edited by kneiel; Sep 23rd, 2009 at 9:46 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,231
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 576
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: writing variable contents to a specific line in a file

 
0
  #2
Sep 24th, 2009
Use -i for in-place replacement and do this:
Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:/tmp$ cat >> file << _EOF_
  2. > line1
  3. > line2
  4. > line3
  5. > line4
  6. > line5
  7. > _EOF_
  8. sk@sk:/tmp$ sed -i '3 s/.*/Your Replacement Here/g' file
  9. sk@sk:/tmp$ cat file
  10. line1
  11. line2
  12. Your Replacement Here
  13. line4
  14. line5
  15. sk@sk:/tmp$

Notice the sed -i '3 -- the 3 is the line number.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 63
Reputation: cfajohnson is an unknown quantity at this point 
Solved Threads: 13
cfajohnson cfajohnson is offline Offline
Junior Poster in Training

Re: writing variable contents to a specific line in a file

 
0
  #3
Sep 27th, 2009
Originally Posted by sknake View Post
Use -i for in-place replacement and do this:
Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:/tmp$ sed -i '3 s/.*/Your Replacement Here/g' file

Note that the -i option to sed is not standard, and many versions do not have it.

Those that do either require or can take an argument with a backup suffix to save the original file -- a good thing, especially if you are not familiar with using sed .
Chris F.A. Johnson
http://cfajohnson.com
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC