1. Overview

In this tutorial, we’ll learn how to remove the last character in a file. While the problem looks easy, there are a few things to keep in mind. We’ll look at multiple ways to solve this problem.

2. The Sample File

Let’s look at the contents of the sample file we’ll be using. We can do this using the cat command:

$ cat sample.txt
This is a file
This file has two lines

Our sample file sample.txt has two lines of text.

3. Removing the Last Character of a File

Let’s now look at a few prominent solutions for the problem of removing the last character of a file:

3.1. Using the sed Command

The command sed performs file operations like insert, delete, search, find, and replace.

Let’s see how to chop the last character in a file using sed:

$ sed '$ s/.$//' sample.txt
This is a file
This file has two line

The sed command above utilizes regex substitution to remove the last character of a file. Here, the first $ points to the last line within the file, the “.” (dot) refers to any character and the last $ points to the end of the line. Thus .$ slashes the last character only.

The $ s/.$// function call executes on the endmost line of the file. It replaces the last character (s) with an empty string. In this way, it completely takes off the last character of the file.

The command sed starts with reading the entire file. So this approach takes a lot of time for large files.

The above sed command does not modify the actual file.

The option -i in the sed command modifies the actual sample.txt file:

$ sed -i '$ s/.$//' sample.txt
$ cat sample.txt
This is a file
This file has two line

But, the above command does not work if the actual file is a symlink.

The sed command is often used to delete a character since it is installed in every Linux system by default.

3.2. Using the truncate Command

The command truncate contracts or expands a file to a given size.

We can use it to remove the last character in a file:

$ truncate -s -1 sample.txt 
$ cat sample.txt
This is a file
This file has two line

The truncate command with option -s -1 reduces the size of the file by one by removing the last character s from the end of the file.

The command truncate takes very little time, even for processing large files.

3.3. Using the ex Command

The command ex points to the Linux text editor. The ex means extended.

Let’s see how to remove the last character in a file using the ex:

$ printf '%s\n' '$' 's/.$//' wq | ex sample.txt
$ cat sample.txt
This is a file
This file has two line

The command printf outputs the content of the file in %s string format specifier. The \n is the escape sequence to print in a new line.

Here, the “.” (dot)  refers to any character, and the $ indicates the last line within the file. Thus .$ deletes the last character only.

The s/.$// function call gets applied to the last line of the file. It replaces the last character with an empty string. In this way, it removes the last character of the file.

The command wq saves and quits the text editor.

The command ex updates the file sample.txt.

3.4. Using the awk Command

The command awk performs text processing and data extraction.

Let’s see how to delete the last character in a file using awk:

$awk '{a[NR]=$0} END {for (j=1; j<NR; j++) print a[j];sub(/.$/,"",a[NR]);print a[NR]}' sample.txt
This is a file
This file has two line

Here, the NR is a built-in variable in awk. It holds the total count of records that are being processed in the sample.txt file.

The function sub is a built-in string function in awk. It substitutes the last character s with an empty string  ” “.  The “.” (dot)  refers to any character and $ specifies the last line within the file.

Thus the awk command above holds the entire file content in an array. Then deletes the last character in the last line of the file. Finally, it prints it on the console.

4. Conclusion

In this article, we learned how to remove the last character in a file using various commands like sed, truncate, ex, and awk.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!