[lug] Output every xth line?
Brad Doctor
bdoctor at voicemail.ps-ax.com
Fri Nov 21 16:20:29 MST 2003
Not a CS grad student? :)
Cut and paste this into a file, then use it like so:
cat <filename> | ./<script_name>.pl <start> <seq>
Remove the ($cnt) if you don't want the line number printed out.
---------------
#!/usr/bin/perl
my $start = shift||die "need starting line\n";
my $mult = shift||die "need sequential line (every <nth>)\n";
my $cnt = 0;
my $i_cnt = 0;
while (<STDIN>) {
$cnt++;
$i_cnt++;
if ($cnt == $start) {
print "($cnt) $_";
$i_cnt = 0;
next;
} # if cnt == start
if ($i_cnt == $mult) {
print "($cnt) $_";
$i_cnt = 0;
} # if i_cnt == mult
} # while
---------------
> 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?
>
> Matt
> --
> I am a theoretical chemist. Fear me!
> Matt Thompson -- http://ucsub.colorado.edu/~thompsma/
> 440 UCB, Boulder, CO 80309-0440
> JILA A510, 303-492-4662
--
Brad Doctor, CISSP
More information about the LUG
mailing list