[lug] Output every xth line?
Tkil
tkil at scrye.com
Fri Nov 21 16:38:39 MST 2003
>>>>> "Joey" == Joseph McDonald <joem at uu.net> writes:
Joey> This should get you started..
Joey> cat /etc/passwd | perl -e '$start = 2; $stop = 6; $c = 0; while(<>) { $c++; if (($c >= $start) && ($c <= $stop)) { print } }'
If you really want to abuse yourself, take a look at the special case
on the ".." operator:
If either operand of scalar ".." is a constant expression, that
operand is implicitly compared to the $. variable, the current line
number.
Which means that you could do:
perl -nwe 'print if 2 .. 6;' /etc/passwd
Sub-lessons:
1. Your "$c" is Perl's built-in "$."
2. The "-n" flag replaces your "while (<>) { ... }" loop.
a. You can still do one-time init with BEGIN { ... } blocks if you
need to.
There's nothing wrong with your code; consider this more a suggestion
to read over the docs for command-line flags (in "perlrun" man page)
and the built-in variables ("perlvar"), and the corner cases of some
operators ("perlop").
t.
More information about the LUG
mailing list