Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

emulate tail with sed

I have a mini-system with only limited number of binaries (bash,cp,cat,sed, ...). I don't have tail command, and I am wondering if I could emulate tail functionality with sed

cat foo.txt | tail -n 10

I know that I can print lines 1-10 with sed cat foo.txt | sed -n '1,10p', but how would I print the last 10 lines?

Answer*

Cancel
5
  • sed -e :a -e '$q;N;11,$D;ba' foo to match the question.
    – Slyx
    Commented Jan 1, 2014 at 10:04
  • I have no idea how it works, but it works perfectly. Thanks a lot. Commented Jan 1, 2014 at 10:13
  • 4
    Could someone explain this wonderful command ? I am looking to have one command that shows begginning and end of a document, and I think this command could help, but I need to add the head part. In fact, the command I was looking for is simply : sed -e '1,11p' -e :a -e '$q;N;11,$D;ba' But I'd still like to understand the tail part !!!
    – Franck
    Commented Jul 16, 2019 at 9:57
  • How might I alias sed -e :a -e '$q;N;11,$D;ba' in .bashrc? When I tried alias tsed="sed -e :a -e \'$q;N;4,$D;ba\'" it gives me: sed: -e expression #2, char 1: unknown command: `'' N: command not found 11,: command not found
    – John Smith
    Commented Apr 19, 2021 at 7:32
  • 2
    Can you add bit explanation to options used? Thanks
    – Pandya
    Commented Nov 11, 2021 at 18:45