Linux 从 CLI 打印文件的最后一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3080693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print the last line of a file, from the CLI
提问by yael
How to print just the last line of a file?
如何只打印文件的最后一行?
回答by RonK
Is it a must to use awkfor this? Why not just use tail -n 1 myFile?
是否必须为此使用awk?为什么不直接使用tail -n 1 myFile?
回答by ghostdog74
Use the right tool for the job. Since you want to get the last line of a file, tail is the appropriate tool for the job, especially if you have a large file. Tail's file processing algorithm is more efficient in this case.
为工作使用正确的工具。由于您想获取文件的最后一行,因此 tail 是适合该工作的工具,尤其是当您有一个大文件时。在这种情况下,Tail 的文件处理算法效率更高。
tail -n 1 file
If you really want to use awk,
如果你真的想使用awk,
awk 'END{print}' file
EDIT : tail -1 filedeprecated
编辑:tail -1 file不推荐使用
回答by Tapan Avasthi
You can achieve this using sedas well. However, I personally recommend using tailor awk.
您也可以使用sed它来实现这一点。但是,我个人建议使用tail或awk。
Anyway, if you wish to do by sed, here are two ways:
无论如何,如果你想通过sed,这里有两种方法:
Method 1:
方法一:
sed '$!d' filename
Method2:
方法二:
sed -n '$p' filename
Here, filename is the name of the file that has data to be analysed.
这里,filename 是包含要分析的数据的文件的名称。
回答by rishabh
Find out the last line of a file:
找出文件的最后一行:
Using sed (stream editor):
sed -n '$p' fileNameUsing tail:
tail -1 fileNameusing awk:
awk 'END { print }' fileName
使用 sed(流编辑器):
sed -n '$p' fileName使用尾巴:
tail -1 fileName使用 awk:
awk 'END { print }' fileName

