if line 5 of a file needs to be replaced with contents of $line, how could we possibly do this using sed ?

Recommended Answers

All 2 Replies

Use -i for in-place replacement and do this:

sk@sk:/tmp$ cat >> file << _EOF_
> line1
> line2
> line3
> line4
> line5
> _EOF_
sk@sk:/tmp$ sed -i '3 s/.*/Your Replacement Here/g' file
sk@sk:/tmp$ cat file
line1
line2
Your Replacement Here
line4
line5
sk@sk:/tmp$

Notice the sed -i '3 -- the 3 is the line number.

Use -i for in-place replacement and do this:

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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.