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.
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018