Output file lines from last to first in Bash

后端 未结 5 1583
说谎
说谎 2020-12-13 00:35

I want to display the last 10 lines of my log file, starting with the last line- like a normal log reader. I thought this would be a variation of the tail command,

5条回答
  •  爱一瞬间的悲伤
    2020-12-13 00:53

    You can do that with pure bash:

    #!/bin/bash
    readarray file
    lines=$(( ${#file[@]} - 1 ))
    for (( line=$lines, i=${1:-$lines}; (( line >= 0 && i > 0 )); line--, i-- )); do
        echo -ne "${file[$line]}"
    done
    

    ./tailtac 10 < somefile

    ./tailtac -10 < somefile

    ./tailtac 100000 < somefile

    ./tailtac < somefile

提交回复
热议问题