English 中文(简体)
How do I get the last non-empty line of a file using tail in Bash?
原标题:

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?

最佳回答

You can use Awk:

awk  /./{line=$0} END{print line}  my_file.txt

This solution has the advantage of using just one tool.

问题回答

Use tac, so you dont have to read the whole file:

tac FILE |egrep -m 1 .

How about using grep to filter out the blank lines first?

$ cat rjh
1
2
3


$ grep "." rjh | tail -1
3

Instead of tac you can use tail -r if available.

tail -r | grep -m 1  . 

if you want to omit any whitespaces, ie, spaces/tabs at the end of the line, not just empty lines

awk  NF{p=$0}END{print p}  file

If tail -r isn t available and you don t have egrep, the following works nicely:

tac $FILE | grep -m 1 .

As you can see, it s a combination of two of the previous answers.

I had problems using other solutions, so I made this.

First, get last 25 lines, assuming at least 1 is not empty. Filter out empty lines, and print out the last line.

 tail -n25 file.txt | grep -v "^.$" | tail -n 1

One major advantage this has, you can show more than 1 line, just by changing the last 1 to, lets say, 5. Also, it only reads last 25 lines of the file.

If you have huge amounts of empty lines, you might want to change the 25 to something bigger, repeating until it works.

Print the last non-empty line that does not contain only tabs and spaces like this:

tac my_file.txt | grep -m 1  [^[:blank:]] 

Note that Grep supports POSIX character class [:blank:] even if it is not documented in its manual page until 2020-01-01.

File may contain other non-visible characters, so maybe using [:space:] may be better in some cases. All space is not covered even by that, see here.

Using ag way:

cat file.txt | ag "S" | tail -1




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签