In this post how to replace and remove one or more lines in a file using sed command with example.

Assue that the below strings saved in replace.txt file

centos=fedora
linux=
ubuntu=kubuntu

Would like to replace the line linux= to unix=linux

Create a new shell file with the name of change.sh and give execute permission to run,
 
# vim change.sh


os="linux"
line="linux="
replace="unix=$os"
sed -i "s/${line}/${replace}/g" replace.txt

output:
 
centos=fedora
unix=linux
ubuntu=kubuntu

d   is specified that delete pattern space

-i  is append and save a file if you have not specified that displays output only.

Remove a line containing a specific string:

sed '/linux/d' replace.txt

sed -i "/log=securefile.log/d"  replace.txt

Remove second line from replace.txt file

sed '2d' replace.txt

sed -i '2d' replace.txt

Remove all empty line from same file:

sed '/^\s*$/d' expty.txt

sed -i '/^\s*$/d' expty.txt

Delete a last line:

 sed '$d' last.txt

 sed -i '$d' last.txt