This is a variation on Gilles' answer for the "if the number of lines to change is small" scenario. Instead of building an inline sed expression, it creates a sed script sent via stdout/stdin pipeline to sed to read with -f -. Doing so avoids any issues with a command-line length limit. You could, alternatively, save the sed script to a "temporary" file and then point sed to that instead.
The other variation I'm bringing in is sed's "c" command, which says to replace the selected line with the given text. The syntax for the "c" command is a little unusual in that it wants a backslash, newline, and then the new text.
sed 's/$/c\\\nNew String/' line-number-file | sed -f - input-file > output-file
The first sed command creates an intermediate sed script as input for the second sed by "replacing" the end of the line ($
) with the "c, backslash, newline, New String" sequence:
3c\
New String
5c\
New String
6c\
New String
To change the text that it's using as a replacement, go inside the first sed section, and replace "New String" with whatever you want.
If you want to replace the text in the original input file, and your sed supports the -i
flag, then you can change the command to:
sed 's/$/c\\\nNew String/' line-number-file | sed -f - -i input-file