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, setsn
to the number of lines to skip (5).NR<=n{next}
- skips processing if the line number is less than or equal ton
.1
- shorthand forprint
everything else.
Solution 5
Print, but delete lines 1 through 2:
sed '1,2d' filename
Related videos on Youtube
Author by
user3905438
Updated on July 09, 2022Comments
-
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 almost 10 yearsThe
{print}
is implied if you don't put anything soawk 'NR>=3' file
will do. -
tripleee almost 10 yearsThe
export
serves no useful purpose here. -
Juan Diego Godoy Robles almost 10 yearsGood one i wasn't aware of that tail functionality
-
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 almost 10 yearsThe shortest would be
awk 'NR>5' file
but since that has already been suggested, I would like to add thatawk
just likesed
supports range operator. So,awk 'NR==5,0' file
will work as well. -
jaypal singh almost 10 yearsMinor nitpick. It will show from 6th line onwards but +1 none the less.
-
jaypal singh almost 10 yearsThanks for the edit. Not sure who down voted your good answer. Hopefully others realize and push it up.
-
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 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 about 7 years@Auspex:
But there is a manpage that answers the OP's question. The tail manpage!
. No, his question specifically mentionedIs there a command for that?
which clearly suggests he doesn't know 'what' the relevant command name is. -
Michael over 5 yearsInstead of filling your answer with rubbish, you could have explained what the syntax represents.
-
Alain Collins about 4 yearsHehe, 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?