There are some situations you want to update the changes to a file without creating temporary files. This can be done using sed.

Today we will some example where we require this requirement.

Example1: How to delete empty lines in a file and update the same file

sed –i ‘/^$/d’ filename

Example2: Recently /home folder is moved to /rhome, please update all the users home directories with /rhome.

Sed –i ‘_/home_/rhome_g’ /etc/passwd

Note: I replaced field separator / with _ in order to eliminate any other changes in the password file.

Example3: Sometimes you will copy a file from windows and found out that a special character is present at end of every line. How can we remove it?

Sed –i -e ‘/^M//g’ filename 

please comment your thoughs on this.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.