[lug] Output every xth line?

Joseph McDonald joem at uu.net
Fri Nov 21 16:21:36 MST 2003


On Fri, Nov 21, 2003 at 03:16:46PM -0700, The Matt wrote:
> Folks, I need your Unix knowledge.  I have an input file that contains
> data that isn't marked nice enough that I can use grep to parse it.
> 
> Rather, I find I need a script that says "starting with line x, output
> that line and every yth line after it".  That is, start at line 5 and
> output line 5, 5+y, 5+2y, &c.  Can you think of a quick shell script
> that can do this?  My attempts have been rather laughable mainly because
> I am neither a perl hacker or a shell script hacker.  (I could make one
> in Fortran 95, but it wouldn't be very portable.)
> 
> Any help for the poor grad student?

This should get you started..

cat /etc/passwd | perl -e '$start = 2; $stop = 6; $c = 0; while(<>) { $c++; if (($c >= $start) && ($c <= $stop)) { print } }'

	--joey





More information about the LUG mailing list