Print just the last line of a file?

孤者浪人 提交于 2020-01-09 08:58:10

问题


How to print just the last line of a file?


回答1:


END{print}

should do it. Thanks to Ventero who was too lazy to submit this answer.




回答2:


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 -n 1 file

If you really want to use awk,

awk 'END{print}' file

EDIT : tail -1 file deprecated




回答3:


Is it a must to use awk for this? Why not just use tail -n 1 myFile ?




回答4:


You can achieve this using sed as well. However, I personally recommend using tail or awk.

Anyway, if you wish to do by sed, here are two ways:

Method 1:

sed '$!d' filename

Method2:

sed -n '$p' filename

Here, filename is the name of the file that has data to be analysed.




回答5:


Find out the last line of a file:

  1. Using sed (stream editor): sed -n '$p' fileName

  2. Using tail: tail -1 fileName

  3. using awk: awk 'END { print }' fileName



来源:https://stackoverflow.com/questions/3080693/print-just-the-last-line-of-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!