Print just the last line of a file?

2019-01-17 06:08发布

How to print just the last line of a file?

5条回答
我只想做你的唯一
2楼-- · 2019-01-17 06:21

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

查看更多
Ridiculous、
3楼-- · 2019-01-17 06:28

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

查看更多
\"骚年 ilove
4楼-- · 2019-01-17 06:33
END{print}

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

查看更多
淡お忘
5楼-- · 2019-01-17 06:35

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

Anyways, 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.

查看更多
ら.Afraid
6楼-- · 2019-01-17 06:39

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

查看更多
登录 后发表回答