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.
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. >
tail -n +1
shows the whole file and tail -n +2
skips first line. strange. The same for tail -c +<num>
.
tail -n +<start number>
, I just tested it. So tail -n +1
won't skip anything, but start from the first line instead.
Aug 22, 2012 at 14:36
tail -n +2
is required to skip the first line on Darwin/Mac OS X as well.
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
sed 1,Xd
where X is the number of initial lines to delete, with X greater than 1.
Dec 24, 2013 at 0:10
tail
to print the last 100000000 lines.
Aug 29, 2018 at 15:06
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.
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.
May 26, 2021 at 19:07
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>
(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, use tail -n +Y <filename>
Dec 24, 2013 at 17:11
A less verbose version with AWK:
awk 'NR > 1e6' myfile.txt
But I would recommend using integer numbers.
awk '!(5 < NR && NR < 10)'
tail
and sed
do not. For example git -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 alias gs
which is really easy to type.
Oct 8, 2021 at 5:59
Use the sed delete
command with a range address. For example:
sed 1,100d file.txt # Print file.txt omitting lines 1-100.
Alternatively, if you want to only print a known range, use the print command with the -n
flag:
sed -n 201,300p file.txt # Print lines 201-300 from file.txt
This solution should work reliably on all Unix systems, regardless of the presence of GNU utilities.
Use:
sed -n '1d;p'
This command will delete the first line and print the rest.
If you want to see the first 10 lines you can use sed as below:
sed -n '1,10 p' myFile.txt
Or if you want to see lines from 20 to 30 you can use:
sed -n '20,30 p' myFile.txt
Just to propose a sed
alternative. :) To skip first one million lines, try |sed '1,1000000d'
.
Example:
$ perl -wle 'print for (1..1_000_005)'|sed '1,1000000d'
1000001
1000002
1000003
1000004
1000005
You can do this using the head and tail commands:
head -n <num> | tail -n <lines to print>
where num is 1e6 + the number of lines you want to print.
This shell script works fine for me:
#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
if (NR >= initial_line && NR <= end_line)
print $0
}' $3
Used with this sample file (file.txt):
one
two
three
four
five
six
The command (it will extract from second to fourth line in the file):
edu@debian5:~$./script.sh 2 4 file.txt
Output of this command:
two
three
four
Of course, you can improve it, for example by testing that all argument values are the expected :-)
cat < File > | awk '{if(NR > 6) print $0}'
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.
I needed to do the same and found this thread.
I tried "tail -n +, but it just printed everything.
The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).
I finally wrote this myself:
skip=5
FILE="/tmp/filetoprint"
tail -n$((`cat "${FILE}" | wc -l` - skip)) "${FILE}"
cat
, but only fixing a link in a comment, that led to a dead page. The original comment must have been deleted. Anyways, thanks for pointing that out.