我如何在Bash中使用尾部文件的最后一个非空行? [英] How do I get the last non-empty line of a file using tail in Bash?

查看:170
本文介绍了我如何在Bash中使用尾部文件的最后一个非空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用的最后一个非空行下的Bash shell?

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

例如, my_file.txt适用是这样的:

您好结果
  HOLA结果
  卓悦结果
  (空行)结果
  (空行)

hello
hola
bonjour
(empty line)
(empty line)

显然,如果我这样做尾-n 1 my_file.txt适用我会得到一个空行。在我来说,我想获得卓悦。我该怎么做呢?

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?

推荐答案

您可以使用awk的:

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

此溶液具有只使用一个工具的优点。

This solution has the advantage of using just one tool.

这篇关于我如何在Bash中使用尾部文件的最后一个非空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆