- [Male Narrator] Hi and welcome to the next video
of section four,
printing lines in the reverse order.
In the previous video,
we learned about printing text
between line number and pattern numbers.
In this video, we'll see how to print lines
in the reverse order.
This is a very simple section.
It may not seem very useful,
but it can be used to emulate the stacked data
of structure in bash.
This is something interesting.
Let's print the lines of a text file in reverse order.
A little hack with awk can do the task.
However, there is a direct command, TAC,
to do the same as well.
TAC is the reverse of CAT.
We will first see how to do this with TAC.
The TAC syntax is as follows.
It can also read from standard in as follows.
In TAC, slash n is the line separator,
but we can also specify our own separator
by using the minus s separator option.
We can do it in awk as follows.
Slash in the shell script
is used to conveniently break a single line command sequence
into multiple lines.
The awk script is very simple.
We store each of the lines into an associative array
with the line number as an index array
and r returns the line number.
In the end, awk executes the end block.
In order to get the last line number,
LNO equals NR is used in the command block.
Hence, it iterates from the last line number to zero
and prints the line starting array in reverse order.
Also, we have successfully learned how to print lines
in the reverse order.
In the next video,
we'll learn how to parse email addresses and URLs from text.