Do you think the way I'm doing it is fine? Is this the most typical way to do it? Share your thoughts please.# Display the last three lines of a text file with line numbers in reverse order# Can come quite in handy when reading log file entriescat -n [FILE] | tac | head --lines=3Test:# Let's create a text file to test our command againstcat > test.txt# Copy and paste these lines into the terminal window and press Ctrl+D to let# the shell know that we're done entering our stuffhousecarboycatlakesealaptoprain# Run the commandcat -n test.txt | tac | head --lines=3# Brief description:# cat -n test.txt | - Pipe the output with line numbers to the tac command# tac |             - Reverse the order and pipe along to the head command# head --lines=3    - Display only the first three lines# Output:     8  rain     7  laptop     6  sea# The number next to the first line in our output (8  rain) should be equal to# the number of lines in the entire file. We can check that using the word count# utility:wc -l test.txt# Output (It's 8 too. Looks like the test passes):8 test.txt
Display the last N lines of a text file with line numbers in reverse order
shell;unix
cat -n is kind of an anti pattern. The cat command is for displaying the content, adding line numbering is a violation of the single responsibility principle. tac is not portable. Although it's quite common in Linux, not so much in UNIX and osx. Instead of head --lines=3 it's more common to use the shorter form head -n 3.I don't quite get why you would want to see the last n lines reversed. If reversing is not important, then a portable alternative to print the last n lines with line numbers could be using awk and tail:awk '{ print NR : $0 }' file | tail -n 3You could write the whole thing in Awk, and also reverse the lines. It will be longer to write, but portable, and solved in a single process rather than a pipeline of multiple commands.