943,836 Members | Top Members by Rank

Ad:
Sep 23rd, 2009
-1

writing variable contents to a specific line in a file

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kneiel is offline Offline
19 posts
since Jul 2008
Sep 24th, 2009
0

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

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by sknake ...
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 .
Reputation Points: 25
Solved Threads: 23
Junior Poster
cfajohnson is offline Offline
193 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: sed substitution with variables
Next Thread in Shell Scripting Forum Timeline: A shell script that will simulate food ordering system in a restaurant





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC