I am trying to print every Nth line out of a file with more than 300,000 records into a new file. This has to happen every Nth record until it reaches the end of the file.
To print every N th line, use
sed -n '0~Np'For example, to copy every 5th line of
sed -n '0~5p' oldfile > newfile
sed’s first ~step address form,
which means “match every step’th line starting
with line first.”
this would print lines 0, 5, 10, 15, 20, 25, …, up to the end of the file.
Of course there is no line 0, so it just prints lines 5, 10, 20, 25, …;
0~5 is just a convenient alternative way of saying
(which prints every 5th line starting with line 5;
i.e., lines 5, 10, 15, 20, 25, …).
For another example of this
(which does not answer the question),
sed -n '2~5p' oldfile
would print lines 2, 7, 12, 17, 22, 27, …, up to the end of the file.