How to print every nth line of a file

Sometimes a scripting language isn't the right tool for the job. I've been working on a piece of code that parses huge logfiles. The test data needed to be representative across 24 hours - a simple slice wouldn't do. I spent a few minutes knocking up a python script to print every 100th line when I stopped and thought 'There's a better way'!

Of all the Unix command line utilities that I've used, I think I'd highlight sed as the most useful and powerful. You can really accomplish quite a terrifying amount with it. To meet my needs, the sed 'script' is seriously trivial.

sed -n '0~100p'

We invoke sed with the -n option, which means only include explicitly printed lines in the output. 0~100 means select every 100th line, beginning at 0. Since 0 doesn't exist, it starts at line 100. It then just prints the line.

I didn't pursue my python script to do the same, but I wouldn't mind betting the sed version is much faster, and given the size of these logs, that a very good thing indeed!

Show Comments