How do I get the last non-empty line of a file using tail in Bash?

前端 未结 7 496
慢半拍i
慢半拍i 2020-12-24 12:26

How do I get the last non-empty line using tail under Bash shell?

For example, my_file.txt looks like this:

hello

7条回答
  •  时光说笑
    2020-12-24 13:14

    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.

提交回复
热议问题