Q. Delete or remove last line from a file.
This can be achieved with SED command.
sed -i '$d' filename
Let me explain this command. SED is a stream editor which is useful for editing stream of characters. here -i option say's to insert the data in to the original file, ie modify original file. $ indicates last line and d for deleting that line and update to the file.
There are other methods to delete last lines
head -n -1 file1 > file2;mv file2 file1
Here I used head command to list all the lines but not the last line, redirected the output of this to a new file then moved file1 to file2.
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.
Latest posts by Surendra Anne (see all)
- 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
- Online training on Linux Bash shell scripting - February 8, 2018