How to display all the lines from the nth line of a file in unix

59,106

Solution 1

you can use tail

excerpt from the manpage:

   -n, --lines=K
         output the last K lines, instead of the last 10; or use -n +K to
         output lines starting with the Kth

for example

tail -n +10 file 

outputs the files content starting with the 10th line

Solution 2

You can use sed:

sed -n '3,$p' file

This selects line 3 to the end and prints it.

Solution 3

To show from 5th line:

awk 'NR>4' file

Solution 4

You can use awk like this:

awk 'BEGIN{n=5}NR<=n{next}1' file
  • BEGIN{n=5} - before file processing starts, sets n to the number of lines to skip (5).
  • NR<=n{next} - skips processing if the line number is less than or equal to n.
  • 1 - shorthand for print everything else.

Solution 5

Print, but delete lines 1 through 2:

sed '1,2d' filename
Share:
59,106

Related videos on Youtube

user3905438
Author by

user3905438

Updated on July 09, 2022

Comments

  • user3905438
    user3905438 almost 2 years

    I want to display all the lines starting from the nth line. Say, print the third line of a file and all the following lines until end of file. Is there a command for that?

  • tripleee
    tripleee almost 10 years
    The {print} is implied if you don't put anything so awk 'NR>=3' file will do.
  • tripleee
    tripleee almost 10 years
    The export serves no useful purpose here.
  • Juan Diego Godoy Robles
    Juan Diego Godoy Robles almost 10 years
    Good one i wasn't aware of that tail functionality
  • paxdiablo
    paxdiablo almost 10 years
    @tripleee: right on the first count (so changed), irrelevant on the second. Whether it's exported or not, I was just showing how to use a shell variable. Have adjusted the answer to remove that anyway.
  • jaypal singh
    jaypal singh almost 10 years
    The shortest would be awk 'NR>5' file but since that has already been suggested, I would like to add that awk just like sed supports range operator. So, awk 'NR==5,0' file will work as well.
  • jaypal singh
    jaypal singh almost 10 years
    Minor nitpick. It will show from 6th line onwards but +1 none the less.
  • jaypal singh
    jaypal singh almost 10 years
    Thanks for the edit. Not sure who down voted your good answer. Hopefully others realize and push it up.
  • Scott Prive
    Scott Prive about 7 years
    @Auspex: Not sure if you were trying to be funny there :-). It's not that SO is faster; there isn't a manpage that would answer the stated goals of the poster. As a decades-long user, hell I didn't know tail did this (I would have turned to what I know, a one-liner awk or Perl snippet).
  • Auspex
    Auspex about 7 years
    @Crossfit_and_Beer Not trying to be funny; I think I wrote that right after seeing somebody else complain that a question could have been answered if the person just read the man page; and I pointed out that since said question didn't seem to have been answered here before, it was perfectly appropriate to have it, and an answer on SO. 'coz this is the place to go for answers. But there is a manpage that answers the OP's question. The tail manpage!
  • Scott Prive
    Scott Prive about 7 years
    @Auspex: But there is a manpage that answers the OP's question. The tail manpage!. No, his question specifically mentioned Is there a command for that? which clearly suggests he doesn't know 'what' the relevant command name is.
  • Michael
    Michael over 5 years
    Instead of filling your answer with rubbish, you could have explained what the syntax represents.
  • Alain Collins
    Alain Collins about 4 years
    Hehe, it represents an answer to the question, which is what the OP asked for. If they wanted to learn sed, this isn't the forum for that, is it?