Print every Nth line

function every() { N=$1; S=1; [ "${N:0:1}" = '-' ] && N="${N:1}" || S=0; sed -n "$S~${N}p"; }
Sometimes commands give you too much feedback. Perhaps 1/100th might be enough. If so, every() is for you. my_verbose_command | every 100 will print every 100th line of output. Specifically, it will print lines 100, 200, 300, etc If you use a negative argument it will print the *first* of a block, my_verbose_command | every -100 It will print lines 1, 101, 201, 301, etc The function wraps up this useful sed snippet: ... | sed -n '0~100p' don't print anything by default sed -n starting at line 0, then every hundred lines ( ~100 ) print. '0~100p' There's also some bash magic to test if the number is negative: we want character 0, length 1, of variable N. ${N:0:1} If it *is* negative, strip off the first character ${N:1} is character 1 onwards (second actual character).
Sample Output
$ seq 100 | every 10

$ seq 100 | every -10

By: flatcap
2015-03-21 23:44:59

1 Alternatives + Submit Alt

  • Thanks to knoppix5 for the idea :-) Print selected lines from a file or the output of a command. Usage: every NTH MAX [FILE] Print every NTH line (from the first MAX lines) of FILE. If FILE is omitted, stdin is used. The command simply passes the input to a sed script: sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin} print no output sed -n quit after this many lines (controlled by the second parameter) -e "${2}q" print every NTH line (controlled by the first parameter) -e "0~${1}p" take input from $3 (if it exists) otherwise use /dev/stdin {3:-/dev/stdin} Show Sample Output

    function every() { sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}; }
    flatcap · 2015-04-03 01:30:36 4

What Others Think

every() { c=$1;for a in $(seq 1 $c $2);do head -$a $3|tail -1;done } every 20 500 file.txt # prints every 20-th from 1 to 500 lines of file file.txt However, could not find an easy way to apply this to output of verbose commands. Maybe eats resources too.
knoppix5 · 348 weeks and 6 days ago
@knoppix5 Hmm... 'head' needs fresh input each loop. The only solution I can think of is to store the input, but that starts getting complicated. As for the efficiency, I agree it's going to be horrendous. 'head' has to read progressively more and more of the file.
flatcap · 348 weeks and 6 days ago
This is a cleaner way to print every 100 lines: awk 'NR%100==0' filename.txt
galaxywatcher · 347 weeks and 1 day ago
Cleaner? Perhaps, but that's because it's not as versatile as the original command :-) It's equivalent to the core: sed -n '0~10p' FILENAME To match my command, you need: every() { N=$1; S=1; [ "${N:0:1}" = '-' ] && N="${N:1}" || S=0; awk "NR%$N==$S"; }
flatcap · 347 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: