Get Last Line of Output in Bash

Bash get last line of output

1. Overview

In this article, we will see how to get last line of output in Bash using tail, awk, sed, mapfile, and head commands with various options.

2. Introduction to Problem Statement

We will use ls -l to list directories and files in long format and get the last line of output using various commands.

For example:

Our goal is to get last last line of the above output. Expected output is:

3. Using tail Command

tail command in the unix system is used to get the last lines of files. By default, it shows the last 10 lines of its input to standard output. We can use tail -n 1 option to get last line of output.

We use the ls- l command to list directories and files from the current directory in the long format and pipe its output to the tail command.

Here, tail -n 1 gives output as the last line because -n parameter is used to specify the number of lines to be extracted from output. Since we want to get only last line, we can use 1 with -n option.

We can capture output to variable using $(..) syntax.

Alternatively, we can also use process substitution as shown below:

Here, we used the process substitution (<(...)), which allowed the command’s output to be treated as the file. In our case, the command was the ls -l.

Using the process substitution syntax, we passed the output of the ls -l command as an input to the tail -n 1 command, which extracted the last line from it and displayed it on the Bash console.

Let’s say we want to get the last line of output, but only for .txt files. We can pipe grep command with -E option with pattern to the output of ls -l command and pipe it to tail -n 1 command.

Grep command can be used as an output to ls -l in all below methods, and we won’t repeat it for the rest of the article.

4. Using awk Command

awk is a powerful scripting language for text processing and is typically used for data extraction.

Let’s use awk to get last line of the output.

Let’s understand more about awk ‘END{print}’.

In the above example, the 'END{print}' represented the awk program, which consisted of the awk pattern-action pair.

Inside awk, END block is a special pattern that triggers action after all input lines have been processed.print without argument is action which instructs awk to print last line of the output.

5. Using sed Command

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 get the last line of output, we can use the $ address, which represents the last line of the output and use it with p command.

While using the sed -n '$p', the -n instructed the sed to suppress printing all lines automatically. In contrast, the $p consists of an address ($, representing the last line) and action (p, printing the matched line).

6. Using mapfile Command

The mapfile command can be used to read entire output of ls -l into array and print the last element of the array.

Here:

  • ls -l generates the file list.
  • The output is piped to mapfile, which stores it in an array named arr.
  • ${arr[-1]} is used to get the last element of the array, which is the last line of the ls -l output.

7. Using head Command

head command is command in Unix-like systems that is typically used to display the beginning of a text file or input.

Here, idea is to reverse the output of ls -l using tac command and get the first line from tac output using head -n 1.

Let’s understand more about the commands.

The tac command is the reverse of the cat command in Bash. It read all lines of input (whether from a file or a standard input; in our case, it was from the previous process in the pipeline) and wrote them line by line to the standard output in the reversed order.

head -n 1 gives the first line of the output as we have reversed ls -l output using tac command; it represents the last line of the ls -l command.

8. Performance

tail -n 1 is the fastest of all the approaches as it is optimized to read from end of the stream, and it should be used in this scenario.

awk and sed options are slower than tail command for large datasets as they process each line and hold the last line in memory, only printing it at the end. This means it reads the entire output, which can be less efficient with very large datasets. It is still good to learn these methods if we want to perform any operation on last line.
For example: Replace word "abc" with "xyz".

mapfile option is less efficient as it reads the output to the array, which can be memory intensive for large datasets.

9. Conclusion

In this article, we explored various ways to get last line of the output. We also compared the performance to choose the best method. Understanding these methods can help us to use method based on our use case and requirements.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *