Print the last line of a file, from the CLI

后端 未结 5 644
旧巷少年郎
旧巷少年郎 2020-12-08 06:33

How to print just the last line of a file?

相关标签:
5条回答
  • 2020-12-08 06:56

    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

    0 讨论(0)
  • 2020-12-08 06:56

    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.

    0 讨论(0)
  • 2020-12-08 07:03

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

    0 讨论(0)
  • 2020-12-08 07:10
    $ cat file | awk 'END{print}'
    

    Originally answered by Ventero

    0 讨论(0)
  • 2020-12-08 07:15

    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

    0 讨论(0)
提交回复
热议问题