How do I get the last non-empty line using tail
under Bash shell?
For example, my_file.txt
looks like this:
hello
hola
bonjour
(empty line)
(empty line)
Obviously, if I do tail -n 1 my_file.txt
I will get an empty line. In my case I want to get bonjour
. How do I do that?
tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.
Use of Tail CommandBy default, the 'tail' command reads the last 10 lines of the file. If you want to read more or less than 10 lines from the ending of the file then you have to use the '-n' option with the 'tail' command.
Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.
Use tac, so you dont have to read the whole file:
tac FILE |egrep -m 1 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With