HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

Display the last N lines of a text file with line numbers in reverse order

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lastthefileorderlinereversewithtextnumbersdisplay

Problem

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 entries
cat -n [FILE] | tac | head --lines=3


Test:

# Let's create a text file to test our command against
cat > 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 stuff
house
car
boy
cat
lake
sea
laptop
rain

# Run the command
cat -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

Solution

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 3


You 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.

Code Snippets

awk '{ print NR ":" $0 }' file | tail -n 3

Context

StackExchange Code Review Q#122863, answer score: 2

Revisions (0)

No revisions yet.