Print a file, skipping the first X lines, in Bash
Solution 1
You'll need tail. Some examples:
$ tail great-big-file.log
< Last 10 lines of great-big-file.log >
If you really need to SKIP a particular number of "first" lines, use
$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >
That is, if you want to skip N lines, you start printing line N+1. Example:
$ tail -n +11 /tmp/myfile
< /tmp/myfile, starting at line 11, or skipping the first 10 lines. >
If you want to just see the last so many lines, omit the "+":
$ tail -n <N> <filename>
< last N lines of file. >
Solution 2
Easiest way I found to remove the first ten lines of a file:
$ sed 1,10d file.txt
In the general case where X
is the number of initial lines to delete, credit to commenters and editors for this:
$ sed 1,Xd file.txt
Solution 3
If you have GNU tail available on your system, you can do the following:
tail -n +1000001 huge-file.log
It's the +
character that does what you want. To quote from the man page:
If the first character of K (the number of bytes or lines) is a `+', print beginning with the Kth item from the start of each file.
Thus, as noted in the comment, putting +1000001 starts printing with the first item after the first 1,000,000 lines.
Solution 4
If you want to skip first two line:
tail -n +3 <filename>
If you want to skip first x line:
tail -n +$((x+1)) <filename>
Solution 5
A less verbose version with AWK:
awk 'NR > 1e6' myfile.txt
But I would recommend using integer numbers.
Eduardo
Updated on December 08, 2021Comments
-
Eduardo over 2 years
I have a very long file which I want to print, skipping the first 1,000,000 lines, for example.
I looked into the cat man page, but I did not see any option to do this. I am looking for a command to do this or a simple Bash program.
-
paxdiablo over 15 yearsOr "tail --lines=+<LinesToSkip> ..." for the readable-commands crowd :-)
-
paxdiablo over 15 yearsNot the most efficient answer since you'd need to do a "wc -l" on the file to get a line count, followed by an addition to add the million :-). You can do it with just "tail".
-
Dana the Sane over 15 yearsI'm not sure, my understanding was that 1e6 would be known at the time of calling. Counting backwards isn't the fastest though.
-
guns about 15 years++ for using awk, which is oh so marginally more portable than tail
-
NickSoft almost 13 yearsin centos 5.6
tail -n +1
shows the whole file andtail -n +2
skips first line. strange. The same fortail -c +<num>
. -
Joel Clark over 12 yearsNick you may be running up against windows style line endings.
-
Andres F. almost 12 years@JoelClark No, @NickSoft is right. On Ubuntu, it's
tail -n +<start number>
, I just tested it. Sotail -n +1
won't skip anything, but start from the first line instead. -
Asclepius over 10 yearsIn the more general case, you'd have to use
sed 1,Xd
where X is the number of initial lines to delete, with X greater than 1. -
Asclepius over 10 yearsThis is somewhat misleading because someone may interpret
(x+1)
literally. For example, for x=2, they may type either(2+1)
or even(3)
, neither of which would work. A better way to write it might be: To skip the first X lines, with Y=X+1, usetail -n +Y <filename>
-
Asclepius over 10 years@Marlon, sorry but that's wrong. That only works for 1d. If, for example, you use it on 2d, you'll delete only line 2. It doesn't delete the range of lines.
-
Marlon over 10 years@A-B-B sorry, meant to say that this was the easiest solution by far which is why I +1 it not trying to correct the author.
-
morgant over 10 yearsI can confirm that
tail -n +2
is required to skip the first line on Darwin/Mac OS X as well. -
osirisgothra about 10 yearsthis must be outdated, but, tail -n+2 OR tail -n +2 works, as with all short commands using getopt, you can run the parameter right next to it's switch, providing that the switch is the last in the group, obviously a command like tail -nv+2 would not work, it would have to be tail -vn+2. if you dont believe me try it yourself.
-
arekolek almost 8 yearsuseful if you need to skip some lines in the middle of the file, e.g.,
awk '!(5 < NR && NR < 10)'
-
Lloeki over 7 yearsWorks for BSD tail too (OS X)
-
G-Man Says 'Reinstate Monica' about 7 yearsThis is a syntax error in bash — in what shell does it work?
-
aamadeo about 7 yearsI run this in bash. The < and > are not part of the command, the name of the file should replace "< File >"
-
Andrew almost 7 yearsOn Solaris, you need to use:
/usr/xpg4/bin/tail
(found this in the man page). -
cerd over 6 yearsMost readily usable answer for both cli and scripting.
-
springloaded almost 6 yearsThis makes more sense if you don't know how long the file is and don't want to tell
tail
to print the last 100000000 lines. -
CSTobey over 5 years
awk 'NR > 6 {print}'
is sufficient... no need for the if or the $0. -
Lon Kaut almost 5 yearsThink I discovered this by accident but both head and tail allow you to omit the
-n
as if it's implied. You can simply<command> | tail +2
-
Tom over 4 yearsbetter than tail imo, since we don't have to know the number of lines to be tail-ed. we just remove the 1st line and that's all
-
Mike Pennington about 4 years@springloaded if you need to know the number of lines in the file, ‘wc -l’ will easily give it to you
-
CervEd about 3 years@Tom you don't need to know the number tailed, to skip the first line use
tail +2
-
Tom about 3 yearsgood point indeed
-
Joel Mellon about 3 years@Lloeki Awesome! BSD head doesn't support negative numbers like GNU does, so I assumed tail didn't accept positives (with +) since that's sort of the opposite. Anyway, thanks.
-
Joel Mellon about 3 yearsAlso, to clarify this answer:
tail -n +2 huge-file.log
would skip first line, and pick up on line 2. So to skip the first line, use +2. @saipraneeth's answer does a good job of exaplaining this. -
gabrielf about 3 yearsActually
awk 'NR>6'
is sufficient as print is the default action block :-) See linuxhandbook.com/awk-command-tutorial for a really good awk tutorial which explains this well. -
Drew Noakes over 2 yearsThis version works in the Cygwin tools that come with Git for Windows, whereas
tail
andsed
do not. For examplegit -c color.status=always status -sb | awk 'NR > 1'
gives a nice minimal status report without any branch information, which is useful when your shell already shows branch info in your prompt. I assign that command to aliasgs
which is really easy to type.