Table of Contents
1. Overview
In Unix-like environments, especially in scripting and programming with Bash, there might be scenarios where we need to remove the last line from a file. This operation is typical in data processing, log file management, and scripting.
2. Introduction to Problem Statement
Imagine we have a text file named test.txt
containing the following 4 lines:
For example:
1 2 3 4 5 6 |
Line 1: This is the first line. Line 2: Here is the second line. Line 3: Now we have the third line. Line 4: Finally, this is the last line. |
Our goal is to remove the last line from the file. Expected output is:
1 2 3 4 5 |
Line 1: This is the first line. Line 2: Here is the second line. Line 3: Now we have the third line. |
The last line ("Line 4: Finally, this is the last line.") should be removed from the file.
3. Using Combination of tail, wc, and truncate Commands
The fastest way to remove last line from file is to use combination of tail, wc, and truncate commands.
1 2 3 |
tail -n 1 test.txt | wc -c | xargs -I {} truncate test.txt -s -{} |
Let’s break down the command to understand how it works:
tail -n 1 test.txt
: This extracts the last line of the file.
wc -c
: This counts the number of characters in the last line.
xargs -I {} truncate test.txt -s -{}
: This uses character count to reduce the file from the end with that many characters using truncate
command.
Remember, the output of the one command is the input for the next command within the pipeline.
This method might be the fastest of all the methods as it avoids reading the whole file. Instead, it directly truncates the file from the end.
Please note that head and tail commands might not work on some GNUs. For example, These commands won’t work on MAC OS sometimes.
4. Using sed Command
The sed
(Stream Editor) is a powerful and versatile text processing tool that performs text transformations on an input stream (a file or input from a pipeline).
To remove last line from the file, we can use following command:
1 2 3 |
sed '$d' test.txt |
on Mac OS, we can use following command:
1 2 3 |
sed -i '' -e '$ d' foo.txt |
In Bash, the $
is a special character representing the last line, while d
is a command used for performing delete operations. So, the $d
together means to match the last line in the given file and delete it.
But the sed
command prints the output to stdin. To write the output to the file directly, we can use -i
option.
1 2 3 |
sed -i '$d' test.txt |
In case we want to back up the original file, we can use -i.bak
option.
1 2 3 |
sed -i.bak '$d' test.txt |
5. Using awk Command
The awk command is a powerful text-processing utility. Let’s use awk to remove the last line from the file.
1 2 3 |
awk 'NR>1 {print prev} {prev=$0}' test.txt |
Let’s understand the awk
expression ('NR>1 {print prev} {prev=$0}'
) below.
- The
NR>1
specified the action for lines where the line number (NR
) exceeds1
. - The
{print prev}
printed the value of theprev
variable, representing the previous line. - The
{prev=$0}
assigned the value of the current line ($0
) to theprev
variable for the next iteration.
We can use -i
option to make changes to file directly.
1 2 3 |
awk -i inplace 'NR>1 {print prev} {prev=$0}' test.txt |
To take the backup of input file, we can use the -v
parameter to set the value of INPLACE_SUFFIX
variable to .bak
value to create a backup of the input file (test.txt
) first and then update it inplace using -i inplace
. The backup file will be named test.Extension.bak
.
1 2 3 |
awk -i inplace -v INPLACE_SUFFIC=.bak 'NR>1 {print prev} {prev=$0}' test.txt |
6. Using head Command
The head
command is used to display the beginning of the file. Using the head command, we can print all the lines except the last line by passing -1
with -n
option.
Therefore, we can use it to remove last line of file in a simple way.
1 2 3 |
head -n -1 test.txt |
But the head
command prints output to stdin. To make changes to the same file, we can create a temp file and move it back to our original one.
1 2 3 |
head -n -1 test.txt > temp.txt && mv temp.txt test.txt |
Here &&
operator is used to execute the second command in case the first command is successful. Therefore, if the head command is successful, then only it will execute mv command.
7. Using tail Command
The tail command is used to display the end of the file but can be used here by reversing the file.
The idea is to reverse the file, remove the last line from the file using tail -n +2
, and reverse it back to get the file in the original sequence.
1 2 3 |
tac test.txt | tail -n +2 | tac |
The tac
command is the reverse of the cat command in Bash. It reads all input lines, and writes them to the standard output in the reversed order.
First, the tac
command reverses the file content, tail -n +2
captures all the lines from 2nd line and last tac command reverses the file to get it back in original format.
Similar to the head command, we can use the following code to make changes to the original file via a temp file:
1 2 3 |
tac test.txt | tail -n +2 | tac > temp.txt && mv temp.txt test.txt |
8. Conclusion
In this article, we explored different ways to remove last line from the file in Bash. Combination of tail, wc, and truncate should be fastest of all the options. Sometimes, we don’t have head and tail command available to us, and we can use awk
and sed
to do it.