2485 shaares
83 private links
83 private links
To print every N th line, use
sed -n '0~Np'
It is written in the form first~step(print). For example, to copy every 5th line of oldfile to newfile, do
sed -n '0~5p' oldfile > newfile
sed -n '2~5p' oldfile
would print lines 2, 7, 12, 17, 22, 27, …, up to the end of the file.
Note: This approach requires GNU sed, as the first ~step address form is a non-portable extension. (Some old versions of GNU sed may require the 5~5 form as opposed to the 0~5 form.)